简体   繁体   English

输入双点而不是逗号

[英]Input double with dot instead of comma

I've got a input in my program expecting a double:我在我的程序中得到了一个输入,期待双倍:

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

but in the uploading system it throws exception java.util.InputMismatchException and I found out that it's because of the system writing a dot as a decimal point.但是在上传系统中它抛出异常java.util.InputMismatchException我发现这是因为系统写了一个点作为小数点。

Is there a way in Java to take a number with a dot as a decimal point instead of comma? Java有没有办法将带点的数字作为小数点而不是逗号?

  1. Switch your Scanner into US Locale:将您的扫描仪切换到美国语言环境:

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

  2. Use Double.parseDouble():使用 Double.parseDouble():

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

I found solution, just had to think a little bit.我找到了解决方案,只需要考虑一下。 I've just taken a next String and parsed it to Double.我刚刚取了下一个String并将其解析为 Double。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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