I do not read the scanner library

-2

I have the following error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at javaapplication2.JavaApplication2.main

    
asked by bya 13.02.2018 в 00:52
source

1 answer

0

The error occurs because you are trying to read an integer, e.nextInt() , and what you entered is a c character. I recommend, for you to avoid this type of errors, that you read the user's input as String and then in the validate code that really corresponds to what you expect. Example:

int num;
String entrada = w.nextLine();
if (entrada.matches("\d+")) {
    // como la entrada es un número entero
    num = Integer.parseInt(entrada);
} else {
    // Pedirle al usuario que vuelva a entrar la información
}
    
answered by 13.02.2018 в 01:11