poll.txtという名前のファイルからデータ セットを取得し、関連データを使用しようとしています。
poll.txtの内容:
CT 56 31 7 Oct U. of Connecticut
NE 37 56 5 Sep Rasmussen
AZ 41 49 10 Oct Northern Arizona U.
ソースコード、ElectoralVotes.java:
import java.io.*;
import java.util.*;
public class ElectionResults {
public static void main(String[] args) throws FileNotFoundException {
int dVotes = 0;
int sVotes = 0;
Scanner scanner = new Scanner(new File("poll.txt"));
String[] nonDigits = new String[29];
int[] Digits = new int[10];
int i = 0;
int j = 1;
while (scanner.hasNextLine()) {
while (scanner.hasNext()) {
nonDigits[i++] = scanner.next();
}
i = 0;
while (j <= 3) {
Digits[i] = Integer.parseInt(nonDigits[j]);
i++;
j++;
}
if (Digits[0] > Digits[1]) {
sVotes += Digits[2];
} else {
dVotes += Digits[2];
}
scanner.nextLine();
}
}
}
ただし、プログラムを実行すると、例外が発生する前に使用されている行は1つだけです。
Exception in thread "main" java.util.NoSuchElementException: No line found error.
「scanner.nextLine();」を動き回ってみました 役に立たない声明。nextLine を要求しなくてもプログラムは正常に動作しますが、明らかにそれが必要であり、何が問題なのかわかりません。