PyVas
PyVas

Reputation: 49

Scanning double with Scanner in Java from Console

I want to read a double number from standard input, but I always get this exception:

java.util.InputMismatchException

import java.util.Scanner;

public class ScanDouble {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double d = scan.nextDouble();
        System.out.println("Double: " + d);
    }
}

If the input is integer it's okay, but when double, I get the exception.

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.nextDouble(Scanner.java:2413) at ScanDouble.main(ScanDouble.java:10)

Upvotes: 0

Views: 5856

Answers (1)

Jens
Jens

Reputation: 69440

Think it is a Problem with the decimal separator. Try the input 10,0

If you want to scan the value with dot, set the locale to locale UK:

Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.UK);
double d = scan.nextDouble();
System.out.println("Double: " + d);

Upvotes: 3

Related Questions