Exception InputMismatchException

2

Because this piece of code throws me an error:

package com.gmail.brunodiazmartin5;

import java.util.Scanner;

public class MiClase {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    double d = sc.nextDouble(); //Excepcion si introduzco 452.65487
                                //java.util.InputMismatchException

    double d2 = 452.65487; //Y esto que es el mismo valor no da error
}
}
    
asked by bruno Diaz martin 10.10.2017 в 16:39
source

1 answer

1

Looking at the documentation I see that the nextDouble method can be applied to Scanner 's but the problem is that if the content does not fit with the regex that validates whether or not double , the result is the exception of InputMismatchException .

If you specify the locale with sc.useLocale(Locale.US); then your variable sc if you can use nextDouble when carrying point and do not eat.

Scanner sc = new Scanner(System.in);
sc.useLocale(Locale.US);
double d = sc.nextDouble(); 
    
answered by 10.10.2017 / 17:00
source