[英]Double accepted with comma instead of dot
I am unable to enter doubles value in my program from my bash terminal.我无法从我的 bash 终端在我的程序中输入双精度值。 Below is the code I used to figure out why it is happening.下面是我用来弄清楚为什么会发生的代码。
This is my code for the test:这是我的测试代码:
import java.util.*;
public class DoubleTest {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
double n = sc.nextDouble();
System.out.println(n);
}
}
With the following input 5, I get the expected results.使用以下输入 5,我得到了预期的结果。
user $ java DoubleTest
5
5.0
Now to the interesting part.现在到有趣的部分。 I input a Double as declared in the code, and this happens:我输入了代码中声明的 Double ,这发生了:
user $ java DoubleTest
5.0
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at DoubleTest.main(DoubleTest.java:6)
And when giving a value of 5,0 - this is the result:当给出 5,0 的值时 - 结果如下:
user $ java DoubleTest
5,0
5.0
So it seems that the .
所以看起来.
(dot) is not working, but instead ,
(comma) is. (dot) 不起作用,但,
(comma) 是。
I already checked my locale, but that shouldn't be the problem.我已经检查了我的语言环境,但这不应该是问题。
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
Your Scanner
is probably using the Denmark
Locale
as it uses ,
to format double variables instead of US default .
您的Scanner
可能正在使用Denmark
Locale
,
格式化双变量而不是美国默认值.
. .
Here is how you can check it (I tried locally forcing my Scanner to use Denmark Locale and it behaves the same way as in your experiments):这是您可以检查它的方法(我尝试在本地强制我的扫描仪使用丹麦语言环境,它的行为与您的实验相同):
Scanner sc = new Scanner(System.in);
System.out.println(sc.locale().getDisplayCountry());
If this is your case, you need to set it to use the US Locale
or any other you want:如果这是您的情况,您需要将其设置为使用美国Locale
或您想要的任何其他:
Scanner sc = new Scanner(System.in);
sc.useLocale(Locale.US);
System.out.println(sc.locale().getDisplayCountry());
Alternatively, if you need to accept both ,
and .
或者,如果您需要同时接受,
和.
you could simply use a formatter.你可以简单地使用格式化程序。 Here is one example with NumberFormat
using France
as Locale, which formats correctly both ,
and .
这是一个使用France
作为区域设置的NumberFormat
示例,它的格式正确,
和.
: :
NumberFormat numberFormat = NumberFormat.getInstance(Locale.FRANCE);
double doubleComma = numberFormat.parse("5,0").doubleValue();
double doubleDot = numberFormat.parse("5.0").doubleValue();
In your code you would do something like:在您的代码中,您将执行以下操作:
public static void main (String[] args) {
try{
Scanner sc = new Scanner(System.in);
NumberFormat numberFormat = NumberFormat.getInstance(Locale.FRANCE);
double n = numberFormat.parse(sc.next()).doubleValue();
System.out.println(n);
} catch (ParseException e) {
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.