2

テキスト(file.txt)ファイルがあります。このファイルはスキャナー内にロードされます。

ファイルにはさまざまな行があり、各行には2つのdoubleと1つの整数値がスペースで除算されています。

このような:

1.0 1.2 2
0.9 1.0 10
50.4 9.2 20

3つの配列を満たす必要があります。

各配列は列に対応します。何かのようなもの:

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)例外が発生します。

私は何が間違っているのですか?

ヒントありがとうございます。

4

3 に答える 3

3

Is it something related to Locale you are parsing doubles ?

Citation from 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.

So, for example, this could help:

scanner.useLocale(Locale.ENGLISH);
于 2012-05-31T10:18:08.487 に答える
1

うまくいく場合は、そのタイプであるため、sc.next()出力を解析する必要があります。StringInputMismatchException

試す:

for (int i=0;j<3;i++) {
  String s;
  s = sc.next();
  x[i] = Double.parseDouble(s);
  s = sc.next();
  y[i] = Double.parseDouble(s);
  s = sc.next();
  z[i] = Double.parseDouble(s);
}
于 2012-05-31T10:08:09.233 に答える
0

When you execute the program, try a comma instead of a dot:

1,0 1,2 2

0,9 1,0 10

50,4 9,2 20
于 2014-02-05T21:25:15.930 に答える