I am stuck in an infinite loop with this piece of code. The program generates endless lines of "Enter number 1> Please enter a number." when an invalid input like "a" is entered instead of an integer. I don't know what's wrong with my boolean variable, everything seems fine to me. Please check it out, thank you so much.
import java.util.*;
public class Adder{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
boolean correctInput=false;
while(!correctInput){
try{
System.out.print("Enter number 1> ");
int num1=sc.nextInt();
System.out.print("Enter number 2> ");
int num2=sc.nextInt();
System.out.println("Sum = "+(num1+num2));
correctInput=true;
}
catch(InputMismatchException e){
System.out.println("Please enter a number.");
correctInput=false;
}
}
}
}