-1

I do not understand what is wrong here:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner cl = new Scanner(System.in);
        double a = cl.nextDouble();
        System.out.print(a);

    }
}

I get this error whenever I type x.x:

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Main.main(Main.java:5)
  • You can not supply alpha characters (like `x`) to the **Scanner#nextDouble()** method other than `+`, `-` and or course `.`. Numerical Digits only. You need to make sure there is no whitespace as well. – DevilsHnd - 退職した Oct 17 '22 at 20:23

1 Answers1

0

Scanners default to the system locale. Your system locale is set to Dutch, or some other locale that uses commas as decimal separator. You have 2 options:

  1. Type 5,5 instead.
  2. Immediately after making a new Scanner, configure it to your liking. THat involves calling cl.useLocale(Locale.ENGLISH);, and probably also cl.useDelimiter("\\R") (which means: The user is expected to hit enter after every input. Versus the mostly useless 'any amount of whitespace' default delimiter).
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72