ここで何か助けが得られることを願っています。
これは私のファイルのテキストです:
Name: John
Name: Peter
Name: Sarah
Place: New York
Place: London
Place: Hongkong
たとえば、次の arraylist にテキスト ファイルの名前のみを追加するにはどうすればよいですか? これまでのところ、私は...そして、場所を含むすべてを配列リストに追加しています!
private ArrayList<Name> names = new ArrayList<>();
public void load(String fileName) throws FileNotFoundException {
String text;
try {
BufferedReader input = new BufferedReader(new FileReader(fileName));
while((text = input.readLine()) != null){
text = text.replaceAll("Name:", "");
names.add(new Name(text));
}
}
catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
最後に、John、Peter、Sarah などの名前のみを Name ArrayLIst に追加する必要があります。
私は何か提案をいただければ幸いです、ありがとう!