Results 1 to 13 of 13
  1. #1
    sassy1 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default 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

    }

    }

  2. #2
    Bmorebob is offline Member
    Join Date
    Mar 2011
    Location
    Manassas, VA
    Posts
    14
    Rep Power
    0

    Default

    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

  3. #3
    Bmorebob is offline Member
    Join Date
    Mar 2011
    Location
    Manassas, VA
    Posts
    14
    Rep Power
    0

    Default

    ...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 08:48 PM. Reason: further clean-up

  4. #4
    Bmorebob is offline Member
    Join Date
    Mar 2011
    Location
    Manassas, VA
    Posts
    14
    Rep Power
    0

    Default

    Also, instead of parallel arrays (which is fine) it might be easier to do a 2-D array instead.

  5. #5
    sassy1 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default while loop

    My 1st post, I apologize if it was not formatted correctly. I tried your suggestion but it never loops??

  6. #6
    Bmorebob is offline Member
    Join Date
    Mar 2011
    Location
    Manassas, VA
    Posts
    14
    Rep Power
    0

    Default

    What do you mean it never loops? What are you trying to have it loop?

  7. #7
    sassy1 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    input window- I enter name
    input window-I enter #
    nothing else, errors about the # string & parse int

  8. #8
    Bmorebob is offline Member
    Join Date
    Mar 2011
    Location
    Manassas, VA
    Posts
    14
    Rep Power
    0

    Default

    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]);
    	}
    }

  9. #9
    sassy1 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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)

  10. #10
    sassy1 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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?

  11. #11
    Bmorebob is offline Member
    Join Date
    Mar 2011
    Location
    Manassas, VA
    Posts
    14
    Rep Power
    0

    Default

    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.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    since you're not doing any operations with the phone number, why not keep them as strings?
    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 ...

    db
    Last edited by DarrylBurke; 03-24-2011 at 03:28 AM.

  13. #13
    sassy1 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Keeping it as a string works, but the last loop does not. What do I need to change to get the last JOption pane to come up?

Similar Threads

  1. for loop problem
    By donal-ctn in forum JCreator
    Replies: 1
    Last Post: 01-31-2011, 08:26 PM
  2. Problem with a for loop
    By ile4 in forum New To Java
    Replies: 3
    Last Post: 12-02-2010, 03:23 PM
  3. For Loop Problem
    By saqib15 in forum New To Java
    Replies: 6
    Last Post: 02-20-2010, 01:59 AM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  5. While-loop problem
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 04:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •