5
I have the following program, where I need to read a whole, print it out for the System.out
However in my code, I want that when the program receives an invalid value as a String, I report the error to the user and still expect to receive an integer.
Here’s the code I got 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 wish that when the user typed a non-integer, he could have another chance to enter an integer number.