问题
I haven't coded in a few years and have been writing simple Java programs to re-familiarize myself with basic principles. However, I'm having trouble getting my do-while loop to act how I want it to. I guess I'm not understanding exactly how do-while loops work, but what I want is to gather user input from System.in as an int, and continue to ask for a valid int input if they enter some other data form. What I have is:
do {
System.out.print("Input: ");
userOption = userInput.nextInt();
} while (!userInput.hasNextInt());
System.out.println("You chose to: " + menuItems.get((userOption - 1)));
This doesn't work for two reasons. First, if I enter a non-int value, it immediately crashes throwing an input mismatch exception. Second, if I do enter a valid int, I always have to enter it twice. I.E., the console will ask for "Input: ", I'll enter say "2", the console will advance to the next line and wait for another input (but without printing anything), and then when I enter a second int it outputs the "You chose to [...]".
I have tried over a dozen different variations that just keep getting more complex, while loops inside do-while, inside if-else, but I'm sure I'm over complicating matters and am missing a simple concept. Any help is very much appreciated!
回答1:
Here is a stand alone example:
import java.util.*;
public class Foo {
public static void main(String args[]){
int userOption;
Scanner userInput = new Scanner(System.in);
System.out.print("Input: ");
while(!userInput.hasNextInt()){
userInput.next();
System.out.print("Error: Please enter an integer\nInput: ");
}
userOption = userInput.nextInt();
System.out.println("You chose to: " +userOption);
}
}
We ask for input the first time and while we can't find an integer we're going to display an error message and discard whatever was entered by user (userInput.next()
, see also: https://stackoverflow.com/a/15973109/1063730).
回答2:
Well, in order to avoid crashing when it's not an integer, load it into a String
first, then let your integer value = parseInt(yourInputAsString);
.
As for the looping structure and double input, you may want to look into the exact behavior of userInput.hasNextInt();
. I would suggest docs.oracle.sun on whatever kind of object that userInput is.
Or are you required to use userInput object's method? I'm not really sure what that is, can you clarify?
Hope this helps a bit at least.
回答3:
It is not crashing, you get exception when the entered value was not int (you asked for int, there was no int, so java could only throw exception). You need to enter int value twice, becauce first int that you enter you read in the userOption statement, so do while conditions needs a new int value to be entered to get the condition true and quit.
Now, knowing it, you should be able to fix your program arround.
In the loop, check if there is a nextInt, if it is consume it (set your variable), if it is not int, read the input and discard it (I guess you use scanner, so nextLine will do the job), you can print out prompt to the user saying that int is required.
I am not writing the exact code as obviously you want to practise. Hint, I would start with while(!input.hasNextInt) {...} Good luck.
P.S. The only time I see do while in Java code is when someone just starts.
来源:https://stackoverflow.com/questions/28664673/java-do-while-loop-simple-issue