Java scanner class won't take double

Java的扫描仪类不需要双

Tags: java
标签: java

问题 (Question)

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.

试图读取键盘输入,但允许小数。

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);
    }

}

我试着与没有声明变量为双打第一。它工作正常阅读的整数时,但双打给inputmismatchexception误差。

最佳答案 (Best Answer)

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.

这可能是一个现场问题。(不知道,不过,既然你没有提供任何样品输入。它也将有助于了解你的默认语言。)你可以设置区域,应使用通过调用扫描仪Scanner.useLocale(Locale locale)。看到论“局部数”在Scanner文件

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/16925944