anhbuiask
anhbuiask

Reputation: 63

Java error : "Exception in thread "main" java.util.InputMismatchException"

I am a newbie with Java and I'm making a very simple Java program like this:

package exercise7; 
import java.util.Scanner;

public class Exercise7 {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a number between 0.0 and 1.0.");
        keyboard.nextDouble();
    }
}

My problem is, when I enter 0.1 for example, they said to me that I had this problem:

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 exercise7.Exercise7.main(Exercise7.java:30)C:\Users\Anh Bui\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1BUILD FAILED (total time: 7 seconds)

I am very confusing, because I thought that nextDouble could mean 0.1 or something like that? Could you please help me? Thank you very much.

Upvotes: 0

Views: 264

Answers (1)

itwasntme
itwasntme

Reputation: 1462

The reason between this is just between using 1.1 or 1,1 in different zones.

The way the Scanner reads the nextDouble is related to your Locale or Zone setting (which, if not overrided, are loaded from system).

For example, I'm in Poland and my delimiter for decimal or floating point numbers is , and regardless the Java standard syntax is to use dot(.), if I input some number with . I'll also get InputMismatchException, but when I use ,, e.g. 0,6 it finishes flawlessly.

In the same time, regardless of the Zone or Locale, the valueOf or parseDouble methods from Double class are using . as floating point separator.

Scanner class methods starting with next... are based on the built-in patterns for discovering the proper values. The discovery pattern is build with the common/regular way of writing numbers/values (of the specified type) in your country/zone.

This is very similar to the Data types, which differs even more on their zone of use.

One of the ways of handling InputMismatchException here is to catch this exception and require the user to provide the number again, similarly to this question.
Your case is a bit different from the input of natural number (because 1.1 and 1,1 should indicate the floating point number.
The simplest way of handling those two separators for decimal numbers would be to read the input as String, replace the wrong separator and parse to double, like:

static double getDouble(String doubleS) {
    doubleS = doubleS.replace(',', '.');
    try {
        return Double.parseDouble(doubleS);
    } catch (NumberFormatException e) {
        e.printStackTrace();
        throw e;
    }
}

Thanks to @Andreas comment, the most appropiate way of handling this would to require the user to provide a decimal number with . separator, because the printed message (before input) has the separator in its numbers.
This could be done with setting the type of Scanner object locale before number input like:

Scanner keyboard = new Scanner(System.in);
keyboard.useLocale(Locale.US); //this sets the locale which has below separator
System.out.println("Enter a number between 0.0 and 1.0.");

Upvotes: 3

Related Questions