int i;
Scanner scan = new Scanner(System.in) {
i = scan.nextInt();
}
What I want to do is to catch the error in scanner when a user inputs a character instead of an integer. I tried the code below but ends up calling for another user input (bec. of calling another scan.nextInt() in assigning value to i after validating the first scan.nextInt() of numeric only):
int i;
Scanner scan = new Scanner(System.in) {
while (scan.hasNextInt()){
i = scan.nextInt();
} else {
System.out.println("Invalid input!");
}
}