Exception InputMismatchException


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

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

1 answer


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);
double d = sc.nextDouble(); 
answered by 10.10.2017 / 17:00