Scanner.nextDouble():用“。”读取double。(Scanner.nextDouble(): reading double with “.”)

编程入门 行业动态 更新时间:2024-10-27 20:25:30
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);

更多推荐

本文发布于:2023-08-05 15:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:nextDouble   Scanner   reading   double

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!