First of all I want to address that I'm a beginner with exceptions, therefore I'm having trouble.
I'm working on a code that tries to detect if a number is an integer or not using try catch. So if the user puts something that's not an integer, let's say the letter 'a', I want the code to ask for another value. In other words, for anything that's not an integer I want to ask the user to put another value.
Code 1:
I've tried to use a detector to see if the user input is and Integer or not
static Scanner kbd = new Scanner(System.in);
    public static void LecturaValidada(){
        int n;
        int esEntero=-1;
        while(esEntero<=0){
            try{
                System.out.print("Introduce un valor: ");
                n = kbd.nextInt();
                esEntero=1;
            } catch(InputMismatchException e){
                System.out.println("Usted ha introducido un caracter/frase, no un entero.");
                esEntero=-1;
            } catch(NumberFormatException e){
                System.out.println("Usted ha introducido un número que no es un entero.");
                esEntero=-1;
            }
        }
        
        System.out.println("Usted SI ha introducido un entero.");
  }
Code 2: Same as Code 1, but with do while
public static void LecturaValidada(){
        int n;
        int esEntero=-1;
        do{
            try{
                System.out.print("Introduce un valor: ");
                n = kbd.nextInt();
                esEntero=1;
            } catch(InputMismatchException e){
                System.out.println("Usted ha introducido un caracter/frase, no un entero.");
                esEntero=-1;
            } catch(NumberFormatException e){
                System.out.println("Usted ha introducido un número que no es un entero.");
                esEntero=-1;
            }
        } while(esEntero == -1);
        
        System.out.println("Usted SI ha introducido un entero.");
}
I've also been playing with boolean values
In the end, I get 1 of 2 outputs:
- Output 1: It will detect that the value put by the user is not an integer and it will not ask for another value
- Output 2: It will constantly appear this message: "Introduce un valor: Usted ha introducido un caracter/frase, no un entero."
 
     
     
    