How to make a loop to ask an input between 2 to 10 and that is not string? (Java)

0

I would like to find a way to ask the user to insert a number between 2 and 10, if the user inserts a different number than an error number and asks it again and if the user inserts a letter that is also error and returns to ask. What I have been able to do and what I have found are several ways, but on the one hand it gives an error in the sense that it detects that it is not among those figures, it gives you an error message and you insert a letter and it does not return to a loop.

    public static void main(String[] args) {
        //kb = keyboard (teclado)
        Scanner kb = new Scanner(System.in);
        boolean out = true;
        //n = number
        int n = 0;

        //loop
        while (out) {
            System.out.println("Inserta un número entre 2 y 10.");

            if (kb.hasNextInt()) {
                n = kb.nextInt();
                if ((n >= 2 && n <= 10)) {
                    out = false;
                } else {
                    System.out.println("Por favor inserta solo entre 2 y 10");
                    kb.next();
                }
                out = false;
            } else {
                System.out.println("No es un número, vuele a intentarlo!");
                kb.next();
            }
        }
        out = true;
    }
}

Another solution found on forums would be this ... but make the mistake you make, always telling you the same phrase.

do {
    System.out.print("Choose a number between[2-10]");

    while(!kb.hasNextInt()){
        kb.nextLine();
        System.out.println("It's not a valid number.");             
    }
    n = kb.nextInt();
} while ((v<=1 || v>=11));

And finally another more complex solution would be this

    final Scanner kb = new Scanner(System.in);
    Integer temp = null;

    while (temp == null) {
      System.out.println("Enter a number between 2 and 10.");
      final String input = kb.nextLine();
    try {
     final Integer value = Integer.valueOf(input);
     if (value >= 2 && value < 10) {
        temp = value;
     } else {
        System.out.println("Bad number, try again");
     }
    } catch (NumberFormatException ex) {
      System.out.println("Not a number, try again");
    }
    }
    int n = temp;

To see what possible solution can be found.

    
asked by 04.11.2017 в 18:34
source

2 answers

0

Here I put another solution more clear and to the point according to what you are talking:

  

I would like to find a way to ask the user to insert a number between 2 and 10, if the user inserts a different number than an error number and asks it again and if the user inserts a letter that is also error and returns to ask.

public static void main(String[]args){
   Scanner in = new Scanner(System.in);
   boolean op = true;
   String val = null;
   do{
      System.out.println("Ingresa un número entre 2 y 10");
      val = in.next();
      if(Integer.valueOf(val)){
         if(val >= 2 && val <= 10){
            op = false; 
         }else{
            System.out.println("El valor ingresado no se encuentra entre los números permitidos");
         }
      }else{
           System.out.println("El valor ingresado no es un número");
      }
   }while(op);
}
    
answered by 04.11.2017 в 21:06
-1

My solution:

    Scanner kbd = new Scanner(System.in);

    int n = 0;
    boolean out = false;

    while (!out) {
        try {

            n = kbd.nextInt();

            if (n < 2 || n > 10) {
                System.out.println("El número debe estar entre 2 y 10");
            } else {
                out = true; // Saldrá del bucle
            }

        } catch (InputMismatchException e) { // Si se produce esta excepción es que no ha escrito un número entero
            System.out.println("No has introducido un número");
            kbd.nextLine(); // Limpiamos el salto de línea
        }

    }
    
answered by 05.11.2017 в 01:16