ファイルからの読み取りに助けが必要です: 私のファイルはまさにそのように見えます:
2,
Bob,
1,
Hand, 10.0, Broken,
John,
Leg, 20.0, Broken,
Hand, 10.0, Broken, //this comma has to be here
このデータをボブとジョンが格納されている配列にロードし、傷害を arrayLists にロードする必要があります。
デフォルトの患者コンストラクターは、患者ごとに arrayList を作成します。
私のコードは次のようになります。
public void load(File f) {
try {
BufferedReader br = new BufferedReader(new File Reader(f));
String nextLine = br.readLine();
while (nextLine!=null) {
StringTokenizer st = new StringTokenizer(nextLine, ",");
int numberOfPatients = Integer.praseInt(st.nextToken());
for (int j=0 j<numberOfPatients; j++) {
String name = st.nextToken(); // This is the place where I get NoSuchElementException
int age = Integer.parseInt(st.nextToken());
this.patients[j-1] = new Patient(name,age);
int numberOfInjuries = Integer.parseInt(st.nextToken());
for (int i=0; i<numberOfInjuries-1; i++) {
String type = (st.nextToken());
if (type.equals("Leg")) {
int cost = Integer.parseInt(st.nextToken());
String injury = st.nextToken();
// addInjury - function to add new injury to the arrayList
this.patients[j].addInjury(new Leg(cost, injury));
} else if (type.equals("Hand")) {
int cost = Integer.parseInt(st.nextToken());
String injury = st.nextToken();
this.patients[j].addInjury(new Hand(cost, injury);
}
nextLine = br.readLine();
}
br.close();
} catch(Exception ex) {
System.out.println(ex);
System.exit(1);
}
}
java.util.NoSuchElementException を返します。誰かが私を助けることができれば、私はそれを感謝します! どうも!