I have something like this:
Scanner in=new Scanner(System.in);
int rounds = 0;
while (rounds < 1 || rounds > 3) {
System.out.print("How many rounds? ");
if (in.hasNextInt()) {
rounds = in.nextInt();
} else {
System.out.println("Invalid input. Please try again.");
System.out.println();
}
// Clear buffer
}
System.out.print(rounds+" rounds.");
How can I clear the buffer?
Edit: I tried the following, but it does not work for some reason:
while(in.hasNext())
in.next();
question from:
https://stackoverflow.com/questions/10604125/how-can-i-clear-the-scanner-buffer-in-java 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…