Scanner.nextDouble():用“。”读取double。(Scanner.nextDouble(): reading double with “.”)
我对Java类Scanner有一点问题:我需要读取一个包含Double值的文件(.dat),但似乎方法Scanner.nextDouble只能识别这样写的数字:1234,5678,带有“,”但不是“。”。 我想知道是否有办法改变它,因为该文件是由另一个软件生成的,所以我不能让它改变“,”为“。”。
谢谢罗宾
I'm have a little problem with the Java class Scanner: I need to read a file (.dat) that contains Double values, but it seems like the method Scanner.nextDouble only recognize number written like this: 1234,5678 , with a "," but not with a ".". I wanted to know if there was a way to change that, because the file is generated by another software, so I can't make it change the "," for a ".".
Thanks Robin
最满意答案
看起来您的默认Locale对double值使用基于逗号的小数分隔符。 尝试使用
scanner.useLocale(Locale.ENGLISH);It seems like your default Locale uses a comma-based decimal separator for double values. Try using
scanner.useLocale(Locale.ENGLISH);更多推荐
发布评论