Home>

Solving the problem withCodeGym, I met such a problem -the compiler throws an error:

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 com.company.Main.main (Main.java:11)

I understand something with myIntelliJ IDEA, because the program code was copied from the site, but it still throws the error. Moreover, if I enter an integer, for example36then everything is fine, but if I enter let's say36.0then the compiler immediately throws an error. Who knows what it might be?

Full program code:

package com.company;
import java.util.Scanner;
public class Main {
    private static boolean isHigh;
    private static boolean isLow;
    public static void main (String [] args) {
        Scanner scanner= new Scanner (System.in);
        double bodyTemperature= scanner.nextDouble ();
        isHigh= bodyTemperature >
 37;
        isLow= bodyTemperature <
 36;
        if (isHigh) {
            System.out.println ("high body temperature");
        } else if (isLow) {
            System.out.println ("body temperature low");
        } else {
            System.out.println ("body temperature is normal");
        }
    }
}

Are you sure you are entering a number with a period, not a comma?

AlekseiGaile2021-02-24 00:13:41

@AlekseiGaile yes, through the point

Andrey2021-02-24 00:13:41

@AlekseiGaile Most likely a locale is used where the decimal separator is a comma

Alexandr2021-02-24 00:13:41
  • Answer # 1

    Most likely, you are using a locale in which the decimal separator is not a period. You can when initializingjava.util.Scannerspecify the locale explicitly:

    Scanner scanner= new Scanner (System.in) .useLocale (Locale.US);
    

    Did as you said, everything worked, thanks!

    Andrey2021-02-24 00:13:41