user5376196
user5376196

Reputation:

InputMismatchException reading a decimal number

I get the following error when entering 55.5 as answer to the first prompt. I don't get the error when entering 50.

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 Miles.main(Miles.java:9)
Java Result: 1
import java.util.Scanner;

public class Miles {
    public static void main(String[] args){ 
        Scanner input = new Scanner(System.in); 
        System.out.println ("Enter the amount of water in kilograms");
        double m = input.nextDouble();

        System.out.println ("Enter the initial temprature");
        double initialTempreture = input.nextDouble();

        System.out.println ("Enter the final termprature");
        double finalTemprature = input.nextDouble();

        double q = m * (initialTempreture - finalTemprature) * 4184;

        System.out.print (q);
    }
}

Upvotes: 0

Views: 586

Answers (2)

user5376196
user5376196

Reputation:

Oh thanks! I just found out that it does indeed work with a , as seperator. Ill keep this in mind :)

Upvotes: 0

Tunaki
Tunaki

Reputation: 137309

You are living in a country where the decimal separator is not ..

You should change the locale with Scanner.useLocale(locale):

input.useLocale(Locale.ENGLISH);

This way, Java will recognize . as the decimal separator.

If you want to know the decimal separator you are using in your default locale, you can use:

System.out.println(DecimalFormatSymbols.getInstance().getDecimalSeparator());

Upvotes: 2

Related Questions