あなたはそれに追加することを繰り返し LinkedList<String>
を作成するとしている中
//ファイルマネージャクラス
public class FileManager {
public static void readFile() {
Scanner x;
LinkedList<String> tempList = new LinkedList<String>();
try {
x = new Scanner(new File("Person.txt"));
@SuppressWarnings("unused")
String temp = null;
while (x.hasNext()) {
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
Person person = new Person();
person.addFilePerson(tempList);
Main.addressBook.add(person);
}
} catch (Exception e) {
System.out.println("could't find the file");
}
}
}
// addFilePerson方法。この行を移動する:
LinkedList<String> tempList = new LinkedList<String>();
into while
ループ。代わりに - そして好ましくは、IMO - 異なる部分に対して個別のプロパティを使用します。
// TODO: Consider what happens if the file runs out half way through a person...
while (x.hasNext()) {
Person person = new Person();
person.setFirstName(x.next());
person.setLastName(x.next());
person.setStreet(x.next());
person.setTown(x.next());
person.setTelephoneNumber(x.next());
person.setCity(x.next()); // Or whatever...
Main.addressBook.add(person);
}
ありPerson
のために、「ビルダー」タイプを作成し、Person
自体は不変作るの周りに他のオプションがあり、あなたは別のAddress
を作成することができますタイプ...
ちょうど1行をやったほうが良いと思います。完璧に動作します。 –