简体   繁体   中英

Java scanner class won't take double

Trying to read keyboard input, but allow for decimals.

import java.util.Scanner;

public class TemperaturFormel {

    public static void main(String[] args)
    {
        Scanner userIn = new Scanner(System.in);
//      double temperatur, vindstyrka;
        double temperatur = userIn.nextDouble();
        double vindstyrka = userIn.nextDouble();

        System.out.print(temperatur + "\n" + vindstyrka);
    }

}

I tried both with and without declaring the variables as doubles first. It works fine when reading integers, but doubles give InputMismatchException error.

This is probably a locale issue. (Impossible to know for sure, though, since you didn't supply any sample input. It would also help to know your default locale.) You can set the locale that the scanner should use by calling Scanner.useLocale(Locale locale) . See the discussion on "Localized Numbers" in the Scanner docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM