Results 1 to 13 of 13
Thread: Problem with while loop
- 03-19-2011, 03:40 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Problem with while loop
Can't get my while loop to work. If I move it up it works but not in the place its suppose to. Something to do with promting the numEntered?? Any ideas?
import java.util.*;
import javax.swing.*;
public class PhoneBook
{
public static void main(String[] args)
{
//int num;
String[] friendName = new String[20];
//String numString;
int[] numEntered = new int[20];
String entry = "", friendString = "";
String input = "", numString = "";
int x = 0;
int highestSub = friendName.length - 1;
Arrays.fill(friendName, "zzz");
friendName[x] = JOptionPane.showInputDialog(null, "Enter in the name of your friend or zzz to exit");
numString = JOptionPane.showInputDialog(null, "Enter the phone number of " + friendName[x]);
numEntered[x] = Integer.parseInt(numString);
while(!friendName[x].equals("zzz") && x < highestSub)
{
friendString = friendString + friendName[x] + "\n";
numString = numString + numEntered [x] + "\n";
++x;
if(x < highestSub)
friendName[x] = JOptionPane.showInputDialog(null, "Enter in the name of your friend or zzz to exit");
}
numEntered = x;
JOptionPane.showMessageDialog(null, "You entered in the names and phone numbers of " + numEntered + " friends: \n" + friendString);
//Arrays.sort(friendName, 0, numEntered); //change to sort by name
x = Arrays.binarySearch(friendName, entry);
if(x >= 0 && x < numEntered)
JOptionPane.showMessageDialog(null, "Your friend xx,xxx has the phone number xxx");//need to fix
}
}
- 03-19-2011, 04:37 PM #2
Member
- Join Date
- Mar 2011
- Location
- Manassas, VA
- Posts
- 14
- Rep Power
- 0
1. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Note that the code you posted is NOT a SSCCE on at least two accounts:
-- doesn't compile
-- lots of code unrelated to the stated problem
- 03-19-2011, 05:27 PM #3
Member
- Join Date
- Mar 2011
- Location
- Manassas, VA
- Posts
- 14
- Rep Power
- 0
...you never re-prompted the user for a new number, just their name (look at the if statement in the while loop)
here you go:
Java Code:import java.util.*; import javax.swing.*; public class PhoneBook { public static void main(String[] args) { int num; String[] friendName = new String[20]; int[] numEntered = new int[20]; String entry = "", friendString = ""; String input = "", numString = ""; int x = 0; int highestSub = friendName.length - 1; Arrays.fill(friendName, "zzz"); friendName[x] = JOptionPane.showInputDialog(null, "Enter in the name of your friend or zzz to exit"); numString = JOptionPane.showInputDialog(null, "Enter the phone number of " + friendName[x]); numEntered[x] = Integer.parseInt(numString); while(!friendName[x].equals("zzz") && x < highestSub) { friendString += friendName[x] + "\n"; numString += numEntered [x] + "\n"; x++; if(x < highestSub) { friendName[x] = JOptionPane.showInputDialog(null, "Enter in the name of your friend or zzz to exit"); if (!friendName[x].equals("zzz")) { numString = JOptionPane.showInputDialog(null, "Enter the phone number of " + friendName[x]); numEntered[x] = Integer.parseInt(numString); } } } /*for(int i = 0; i < friendName.length; i++) Debugging check { System.out.println(friendName[i]); }*/ num = x; JOptionPane.showMessageDialog(null, "You entered in the names and phone numbers of " + num + " friends: \n" + friendString); Arrays.sort(friendName, 0, num); entry = JOptionPane.showInputDialog(null, "Whose number would you like to find?"); x = Arrays.binarySearch(friendName, entry); if(x >= 0 && x < num) JOptionPane.showMessageDialog(null, "Your friend " + entry + " has the phone number " + numEntered[x]); } }Last edited by Bmorebob; 03-19-2011 at 07:48 PM. Reason: further clean-up
- 03-19-2011, 07:50 PM #4
Member
- Join Date
- Mar 2011
- Location
- Manassas, VA
- Posts
- 14
- Rep Power
- 0
Also, instead of parallel arrays (which is fine) it might be easier to do a 2-D array instead.
- 03-21-2011, 10:49 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
while loop
My 1st post, I apologize if it was not formatted correctly. I tried your suggestion but it never loops??
- 03-21-2011, 10:59 PM #6
Member
- Join Date
- Mar 2011
- Location
- Manassas, VA
- Posts
- 14
- Rep Power
- 0
What do you mean it never loops? What are you trying to have it loop?
- 03-21-2011, 11:07 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
input window- I enter name
input window-I enter #
nothing else, errors about the # string & parse int
- 03-21-2011, 11:14 PM #8
Member
- Join Date
- Mar 2011
- Location
- Manassas, VA
- Posts
- 14
- Rep Power
- 0
copy and paste the code below into an empty java file...
Java Code:import java.util.*; import javax.swing.*; public class PhoneBook { public static void main(String[] args) { int num; String[] friendName = new String[20]; int[] numEntered = new int[20]; String entry = "", friendString = ""; String input = "", numString = ""; int x = 0; int highestSub = friendName.length - 1; Arrays.fill(friendName, "zzz"); friendName[x] = JOptionPane.showInputDialog(null, "Enter in the name of your friend or zzz to exit"); numString = JOptionPane.showInputDialog(null, "Enter the phone number of " + friendName[x]); numEntered[x] = Integer.parseInt(numString); while(!friendName[x].equals("zzz") && x < highestSub) { friendString += friendName[x] + "\n"; numString += numEntered [x] + "\n"; x++; if(x < highestSub) { friendName[x] = JOptionPane.showInputDialog(null, "Enter in the name of your friend or zzz to exit"); if (!friendName[x].equals("zzz")) { numString = JOptionPane.showInputDialog(null, "Enter the phone number of " + friendName[x]); numEntered[x] = Integer.parseInt(numString); } } } num = x; JOptionPane.showMessageDialog(null, "You entered in the names and phone numbers of " + num + " friends: \n" + friendString); Arrays.sort(friendName, 0, num); entry = JOptionPane.showInputDialog(null, "Whose number would you like to find?"); x = Arrays.binarySearch(friendName, entry); if(x >= 0 && x < num) JOptionPane.showMessageDialog(null, "Your friend " + entry + " has the phone number " + numEntered[x]); } }
- 03-21-2011, 11:35 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Must be me I still get the following;
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at PhoneBook.java.PhoneBook.main(PhoneBook.java:28)
- 03-23-2011, 08:24 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Thanks for all of your help, my problem seems to be that my JOptions Pane does not accept the dashes I try to put in the phone number. How do I do that?
- 03-23-2011, 08:42 PM #11
Member
- Join Date
- Mar 2011
- Location
- Manassas, VA
- Posts
- 14
- Rep Power
- 0
Ohh, well that's because dashes can't be read in as numerical values. The easiest way around it would be to tell the user to not include dashes when inputting the phone number. Or better yet, since you're not doing any operations with the phone number, why not keep them as strings? that would eliminate any requirements.
- 03-24-2011, 02:24 AM #12
Sound advice. Any numerical entities that are not subject to any kind of mathematics should always be stored and treated as Strings. This includes phone numbers, house numbers, social security numbers, employee ID numbers ...since you're not doing any operations with the phone number, why not keep them as strings?
dbLast edited by DarrylBurke; 03-24-2011 at 02:28 AM.
- 03-24-2011, 11:01 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
for loop problem
By donal-ctn in forum JCreatorReplies: 1Last Post: 01-31-2011, 07:26 PM -
Problem with a for loop
By ile4 in forum New To JavaReplies: 3Last Post: 12-02-2010, 02:23 PM -
For Loop Problem
By saqib15 in forum New To JavaReplies: 6Last Post: 02-20-2010, 12:59 AM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
While-loop problem
By jimmy-lin in forum New To JavaReplies: 6Last Post: 11-02-2009, 03:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks