Scanner到底如何解析两倍

Scanner到底如何解析两倍

本文介绍了Java Scanner到底如何解析两倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 7计算机,其控制面板\时钟,语言和区域"为丹麦"

I'm using a Windows 7 machine whose "Control Panel\Clock, Language, and Region" is "Denmark"

根据扫描仪的文档:

但是当我运行代码时:

System.out.println(Locale.getDefault());
Scanner sc = new Scanner("1.0");
sc.nextDouble();

它输出"en_US",然后在sc.nextDouble()处引发java.util.InputMismatchException.当扫描仪初始化为"1,0"时可以使用

It outputs "en_US" and then throws a java.util.InputMismatchException at sc.nextDouble() .It works when the scanner is initialized with "1,0"

但是,如果我明确设置了语言环境:

However, if I explicitly set the Locale:

Locale.setDefault(Locale.US);
System.out.println(Locale.getDefault());
Scanner sc = new Scanner("1.0");
sc.nextDouble();

它输出"en_US",然后解析双精度值.我是否缺少某些内容,或者扫描仪的文档有误?

It outputs "en_US" and then parses the double just fine. Am I missing something, or is the documentation for Scanner wrong?

编辑根据@Perception的建议,我在第一个示例中查看了sc.locale().打印"da_DK".那么,为什么Locale.getDefault()方法返回的结果不是"en_US"?

Edit Following the suggestion of @Perception, I looked at sc.locale() in the first example. It prints "da_DK". So why is it not "en_US", when that is what is being returned by the Locale.getDefault() method?

推荐答案

有两种不同的语言环境类别,一种用于显示,一种用于格式.扫描程序使用Locale.getDefault(Locale.Category.FORMAT),但是如果您调用Locale.getDefault(),则会获得要显示的语言环境. setLocale(Locale)方法同时设置了两者.

There are two different Locale categories, one for display and one for format. The scanner uses Locale.getDefault(Locale.Category.FORMAT) but if you call Locale.getDefault() you get the locale for display. The setLocale(Locale) method sets both.

这篇关于Java Scanner到底如何解析两倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:55