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();
while(in.hasNext()) in.next();
. For some reason that loop does not work. – Caridadcarie