I have this code...
import java.util.*;
public class SwitchExample {
public static void main(String[] args) {
System.out.print("Enter ID: ");
Scanner scanner = new Scanner(System.in);
while (!scanner.hasNextInt()) {
System.out.print("Invalid. Enter again: ");
scanner.next();
}
int number = scanner.nextInt();
System.out.println("Your number was: " + number);
System.out.println("Test message got printed!");
}
}
Typing a valid input is fine, and upon typing an invalid character, it still works as required. However, upon typing an enter key, in either case does not raise an error. Please help on how I can achieve that. I have tried multiple ways but none have worked.