I intend to read two whole numbers while the two data read are not correct. That is, if in any of the two data something that is not an integer is introduced, the do-while loop is repeated again. Next I leave the code:
int a = 0, b = 0;
boolean est;
Scanner sc = new Scanner(System.in);
do {
try {
est = false;
System.out.print("Introduce el primer numero a: ");
a = sc.nextInt();
System.out.print("Introduce el segundo numero b: ");
b = sc.nextInt();
} catch (InputMismatchException exc) {
System.out.println("Error, lo introducido no es valido: " + exc.getMessage());
est = true;
}
} while(est);
The fact is that when you enter something that is not an integer, the loop repeats indefinitely. Where is the problem?
Thanks in advance.