Results 1 to 5 of 5
Thread: Loop error
- 02-18-2011, 11:11 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
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."); } } } }
- 02-18-2011, 11:35 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 02-19-2011, 12:12 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
i see what you're saying but i still cant get it to work. i tried in.reset()
- 02-19-2011, 12:48 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
try reading in the bad data in your last else statement into a String.
Java Code:String badData=in.next();
- 02-19-2011, 12:56 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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 messageelse get int value that was entered and...
if it is outside the range give error messageelse set actualFloor and make done true
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
For loop error
By ShotGunRockets in forum New To JavaReplies: 6Last Post: 04-06-2009, 01:14 AM -
help error in while loop
By iPetey in forum New To JavaReplies: 3Last Post: 04-03-2009, 03:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks