Why input.nextLine() keeps running the catch block when I'm inputting a value which is not an integer? And why it is not running from input.nextInt when I'm inputting an Int value in input.nextLine()?
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionHandlingEg01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = 0;
do{
try{
num = input.nextInt();
if(num != -1) {
System.out.println("You Entered " + num + "\nEnter -1 to exit: ");
}
else {
System.out.println("Program Exited");
}
} catch (InputMismatchException e) {
e.printStackTrace();
System.out.println("Please Enter a number");
input.nextLine();
}
} while (num != -1);
}
}