I have the following program, where I need to read an integer, print it to System.out
However, in my code, when the program receives an invalid value as a String, I want to report the error to the user and still expect to receive an integer.
Here is the code I have today:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
while(true){
System.out.printf("Entre com um número inteiro qualquer: ");
try{
int inteiro = entrada.nextInt();
System.out.printf("Eis a aberração: %d", inteiro);
}
catch(Exception e){
System.out.printf("Você não digitou um número inteiro!");
}
}
}
}
I would like that when the user types a non-integer, he might have another chance to type an integer.