I have a problem when trying to execute try-catch statement inside do while loop.I ask user to first enter letter and then a number and if he enters number correctly the program ends.If he enters letter instead of number the program should say "An error occurred please enter number " and ask user to enter number again but every time i type letter instead of number the program goes into an infinite loop and won't allow me to enter new value. And just goes "An error occurred you must enter number" "Please enter number".
public class OmaBrisem {
public static void main(String[] args) {
Scanner tastatura = new Scanner(System.in);
boolean b = true;
int a = 0;
String r = "";
System.out.println("Please enter a letter");
r = tastatura.next();
do {
try {
System.out.println("Please enter numerical value");
a = tastatura.nextInt();
b = true;
} catch (Exception e) {
System.out.println("An error occured you must enter number");
b = false;
}
} while (!b);
}
}