用点而不是逗号输入double

时间:2018-03-25 17:48:52

标签: java decimal-point

我的程序中有一个输入需要加倍:

Scanner in_num = new Scanner(System.in);
double num1 = in_num.nextDouble();

但是在上传系统中它会抛出异常java.util.InputMismatchException,我发现这是因为系统将点写为小数点。

Java中是否有办法用点作为小数点而不是逗号?

2 个答案:

答案 0 :(得分:1)

  1. 将您的扫描仪切换到美国语言环境:

    Scanner sc = new Scanner(System.in);
    sc.useLocale(Locale.US);
    double d = sc.nextDouble();

  2. 使用 Double.parseDouble():

    BufferedReader reader = new BufferedReader(new inputStreamReader(System.in));
    double d = Double.parseDouble(reader.readLine());

答案 1 :(得分:0)

I found solution, just had to think a little bit. I've just taken a next String and parsed it to Double.

double num1 = Double.parseDouble(in_num.next());