gpt4 book ai didi

java - Scanner.nextDouble : txt file correctly loaded but "InputMismatchException" occurs

转载 作者:行者123 更新时间:2023-11-30 07:25:38 27 4
gpt4 key购买 nike

我有一个文本 (file.txt) 文件。此文件加载到扫描仪中。

文件中有不同的行,每一行中有两个 double 值和一个整数值除以空格。

像这样:

1.0 1.2 2
0.9 1.0 10
50.4 9.2 20

我需要完成三个数组。

每个数组对应一个。像这样的东西:

double[] x = {1.0,0.9,50.4}
double[] y = {1.2,1.0,9.2}
double[] z = {2,10,20}

我尝试使用这段代码:

double x[]= new double [3];
double y[]= new double [3];
double z[]= new double [3];

File f = new File(ClassLoader.getSystemResource(file.txt).toURI());
Scanner sc = new Scanner(f);

for (int i=0;j<3;i++){
x[i] = sc.nextDouble();
y[i] = sc.nextDouble();
z[i] = sc.nextDouble();
}

但是当执行第一个 nextDouble() 时出现“未捕获”(java.util.InputMismatchException)异常。

我做错了什么?

感谢您的任何提示。

最佳答案

你正在解析 doubles 是否与 Locale 有关?

引自 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale. A scanner's initial locale is the value returned by the Locale.getDefault() method; it may be changed via the useLocale(java.util.Locale) method.

因此,例如,这可能会有所帮助:

scanner.useLocale(Locale.ENGLISH);

关于java - Scanner.nextDouble : txt file correctly loaded but "InputMismatchException" occurs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831224/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com