2

Problem

I was messing around with Java today, and bumped into something that I can't fix by myself. I'm trying to get a double value from scanner but always get this error:

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:20)

Source code:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final char ADD = '+';
        final char SUBTRACT = '-';
        final char MULTIPLY = '*';
        final char DIVIDE = '/';

        double num1;
        double num2;

        char operator;

        Scanner sc = new Scanner(System.in);

        num1 = sc.nextDouble();
        num2 = sc.nextDouble();
        operator = sc.next().charAt(0);

        System.out.print(num1 + " " + operator + " " + num2 + " = ");

        switch (operator) {
        case ADD:
            System.out.print(num1 + num2);
            break;
        case SUBTRACT:
            System.out.print(num1 - num2);
            break;
        case MULTIPLY:
            System.out.print(num1 * num2);
            break;
        case DIVIDE:
            System.out.print(num1 / num2);
            break;
        default:
            System.out.println("Error");
            break;
        }
    }
}

My input:

5.2

And then the exception is thrown.

I also have a small video illustrating the problem: https://streamable.com/wes4g1


Minimal example

I was even able to reproduce the issue with a very minimal setup like:

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double value = scanner.nextDouble();
        System.out.println(value);
    }
}

with the input 5.2 or any other decimal.

Inputing an actual integer value, for example just 5 works however.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Yashmerino
  • 121
  • 9
  • Is that your entire input? Your code attempts to read to read two doubles from stdin – Mureinik Sep 11 '21 at 11:42
  • @Mureinik yes that's the entire input, i type 5.2 and the exception is thrown. – Yashmerino Sep 11 '21 at 11:43
  • This can't be the input causing the error as this will perfectly work given the `num1 = sc.nextDouble();` statement. Are you performing any additional keystroke? – tmarwen Sep 11 '21 at 11:45
  • May it be a problem with my eclipse ide or kind of? I don't type anything else than this. Also the thing is that if i type a int number, for example 5, then everything works fine – Yashmerino Sep 11 '21 at 11:47
  • Can't reproduce - I ran this locally and after the 5.2 input it just hangs and waits for the next input, as expected. Can you share some details on how you tun this? – Mureinik Sep 11 '21 at 11:47
  • Voting to close, not reproducible – Zabuzard Sep 11 '21 at 11:48
  • https://streamable.com/wes4g1 Here is a video of what's happening. What kind of details should i share? – Yashmerino Sep 11 '21 at 11:52
  • 1
    Whats the system language set on your machine? For countries like germany you have to input a comma, like `5,2` instead. Try to enter it in the way expected by your current machines country/language settings (locale). If thats the issue and you dont want that, you can easily change the locale that Java uses. – Zabuzard Sep 11 '21 at 12:02

2 Answers2

2

Scanner and Locale

It is not directly obvious from the Javadoc of Scanner#nextDouble but it is Locale-sensitive.

There are various things that influence the way how Scanner expects numbers and text to look like that depend on your current set Locale.

By default, Scanner uses your current systems locale (country and language settings). So you have to enter the numbers in the way they are usually written in your current region.

Decimal separator . vs ,

In your particular case, it is the decimal separator which you entered with . (dot) but Scanner expected a , (comma) instead. This is for example the case with a Moldovan locale, since in Moldovan comma is used instead.

Hence, when you entered 5.2 it did not see any double, as specified by your current Locale. But when you enter 5,2, it works.

You can read more about this in the Javadoc of Scanner, section Localized numbers. Also see this illustration from Wikipedia:

Countries using decimal comma

Change locale

That said, you can use Scanner#useLocale to change the locale Scanner uses. For example:

scanner.useLocale(Locale.ENGLISH);

and then you can also work with 5.2 as input.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
1

All i had to do was using comma instead of a dot, "5,2" instead of "5.2". Big thanks to @Zabuzard. He pointed out the solution.

Yashmerino
  • 121
  • 9