The below program simply reads integers from the console and prints it back. When one enters a non-int (like char or String), the Scanner throws an exception. I tried to handle the exception in the 'try-catch' block and move on to read the next input. After the first non-int input from console, the programs runs into infinite loop. Can someone please help?
public class ScannerTest {
static int i=1;
static Scanner sc;
public static void main (String args[]){
sc = new Scanner(System.in);
while (i!=0){
System.out.println("Enter something");
go();
}
}
private static void go(){
try{
i = sc.nextInt();
System.out.println(i);
}catch (Exception e){
System.out.println("Wrong input, try again");
}
}
}