why isn't this code working as expected?
public class FinalTest {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
int k = 0;
boolean askForInput = true;
while ( askForInput ) {
System.out.print("Enter an integer: ");
try {
k = in.nextInt();
askForInput = false;
} catch (InputMismatchException e) {
System.out.println("ERR: Not an integer!!");
}
}
}
}
nextInt()
tries to scan the input as an int, and if it is not an int, it should throw an exception ERR: Not an integer. What bugs me is, why doesn't it prompt for input again? It just keeps printing the ERR message on screen.