Results 1 to 5 of 5

Thread: Loop error

  1. #1
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Loop error

    im trying to get this loop to work. if i dont enter an integer into floor it will go to the else statement and display: Error, not an integer and keep displaying that. How do i make it go back and ask for int floor again?

    Java Code:
    import java.util.Scanner;
    
    public class ElevatorSimulation2 {
    
    	public static void main (String[] args)
    	{
    		Scanner in = new Scanner(System.in);
    		
    		boolean done = false;
    		while(!done)
    		{
    			System.out.println("Floor: ");
    		
    			if (in.hasNextInt())
    			{
    				int floor = in.nextInt();
    				
    				if (floor == 13)
    				{
    					System.out.println("Error: There is no 13th floor.");
    				}
    				else if (floor <= 0 || floor > 20)
    				{
    					System.out.println("Error: The floor must be between 1 and 20.");
    				}
    				else
    				{
    					int actualFloor = floor;
    					if (floor > 13)
    					{
    						actualFloor = floor - 1;
    					}
    					System.out.println("The elevator will travel to the actual floor " + actualFloor);
    					done = true;
    				}
    			}
    			else
    			{
    				System.out.println("Error: Not an integer.");
    							
    			}
    		}
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    If hasNextInt() returns false you had better print the message and consume the bad input that the scanner is looking at before you call hasNextInt() again. Otherwise that method will keep looking at the bad input and will keep returning false (as you have seen).

    ----------------

    In general with Scanner methods, it pays to read the API documentation carefully taking note of BOTH what is returned AND where the scanner ends up looking after the method has returned.

  3. #3
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    i see what you're saying but i still cant get it to work. i tried in.reset()

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    try reading in the bad data in your last else statement into a String.

    Java Code:
    String badData=in.next();

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    It's not clear why you would use reset() as its documentation clearly says that what it resets are the radix/locale/delimiter settings, none of which you are using.

    The first thing to be done is to have the user actually enter something. Clearly your program must stop and wait for their input before it can do anything else. Sorry, I hadn't noticed that your code wasn't doing this - it is racing ahead and calling hasNextInt() before the user has even had a chance to provide any input.

    Try scrapping the existing while loop and proceeding along the following lines:

    loop while not done
    prompt and get user input (check the nextXXX() methods of scanner)
    if input is not an integer give error message
    else get int value that was entered and...
    if it is outside the range give error message
    else set actualFloor and make done true

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 05:21 PM
  3. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  4. For loop error
    By ShotGunRockets in forum New To Java
    Replies: 6
    Last Post: 04-06-2009, 02:14 AM
  5. help error in while loop
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-03-2009, 04:56 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
  •