Results 1 to 8 of 8
  1. #1
    jdg951 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default problem with loop (maybe?)

    Hey, I have two problems that I can't figure out.
    1. I want the loop to end when I type in "x" (it does this) but I don't want it to print "*** Invalid Entry ***" like this:

    Enter an integer(x to exit): 1
    Enter an integer(x to exit): x

    *** Invalid data entry ***
    Enter an integer(x to exit):

    (loop ends at this point)


    2. If I type in a string after the first entry being correct, it displays:
    *** Invalid data entry ***
    Enter an integer(x to exit):
    *** Invalid data entry ***
    Enter an integer(x to exit):

    obviously I want this to only be displayed once.
    Help would be very greatly appreciated.


    Java Code:
    //input values into array list
    		String aValue = "";
    		do
    		{
    			System.out.print("Enter an integer(x to exit): ");
    			if (scan.hasNextInt())
    			{
    				int _value = scan.nextInt();
    				square.add(_value);
    			}
    			else
    			{
    				aValue = scan.nextLine();
    
    				if (!aValue.equalsIgnoreCase("x"))
    				{
    					System.out.println("\n*** Invalid data entry ***");
    				}
    			}
    		}
    		while(!aValue.equalsIgnoreCase("x"));
    Last edited by jdg951; 12-07-2010 at 05:44 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here,

    Java Code:
    if (!aValue.equalsIgnoreCase("x"))
    				{
    					System.out.println("\n*** Invalid data entry ***");
    				}
    for all non 'x' characters you've print to the console.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    after the int _value = scan.nextLine(); in your first conditional block (for the if), add a scan.nextLine(). Your scanner isn't reading the newline character, so when you try to get another integer, it reads the newline in the else block (since it's not an integer), and then correctly reports the newline as invalid.

    After a bit of my own experimentation, that missing line is causing your first problem as well. You read in an integer, and then, after having more input forwarded to your scanner when the user enters another newline (for the hasNextInt check) it reads the newline, then the invalid string, and correctly reports both as invalid. You'll notice none of your problems occur until you've entered a valid integer.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes it is, expecting (reading) an integer but looking for a character to exit, which ends with an error again.

  5. #5
    jdg951 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    OK I figured it out. I addeda scan.nextLine() and then added a aValue.trim();
    Last edited by jdg951; 12-07-2010 at 08:26 PM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    jdg951 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    I did. Thank you.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. While loop problem
    By mochibon in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 08:21 PM
  2. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  3. While-loop problem
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 03:10 AM
  4. Problem with loop
    By Tykk in forum New To Java
    Replies: 7
    Last Post: 10-04-2009, 10:26 PM
  5. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM

Posting Permissions

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