Here's the code:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Number: ");
if (scanner.hasNextInt()) {
System.out.println(scanner.nextInt());
break;
} else {
System.out.println("Not an integer.");
}
}
scanner.close();
}
When the value is an integer everything works fine, the loop breaks out, however when the input is not an integer I keep seeing this infinitely flooding the terminal:
Number: Not an integer.
Number: Not an integer.
Number: Not an integer.
...
...
...