ここでこれを解決する方法について、素晴らしい提案ができることを願っています。
場所と名前を含むこのテキスト ファイルがあります。名前の詳細は、その人物が訪れた場所を示しています。
Place: New York
Place: London
Place: Paris
Place: Hongkong
1. Name: John
1. Name detail: London
1. Name detail: Paris
1. Name detail: Hongkong
2. Name: Sarah
2. Name detail: London
3. Name: David
3. Name detail: New York
3. Name detail: Paris
これが私のコードの一部です。
private ArrayList<Place> places = new ArrayList<>();
private ArrayList<Name> names = new ArrayList<>();
public void load(String fileName) throws FileNotFoundException {
ArrayList<Place> place = places;
BufferedReader br = new BufferedReader(new FileReader(fileName));
int nameCounter = 1;
int nameDetailCounter = 1;
String text;
try {
while ((text = br.readLine()) != null) {
if (text.contains("Place:")) {
text = text.replaceAll("Place:", "");
places.add(new Place(text));
} else if (text.contains(nameCounter + ". Name:")) {
text = text.replaceAll(nameCounter + ". Name:", "");
names.add(new Name(text, ""));
nameCounter ++;
}
//starting from here!
else if (text.contains(nameDetailCounter + ". Name detail:")) {
text = text.replaceAll(nameDetailCounter + ". Name detail:", "");
for (Name name : names) {
Name nameDetails = findName(name.getName());
Place placeDetails = findPlace(text);
nameDetails.addName(placeDetails);
}
nameDetailCounter ++;
}
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
私の考えは、すべて「1」を選択することです。最初にテキスト ファイルから取得して配列に追加し、すべて「2.」に進みます。それを配列に追加します。
多くの方法を試しましたが、「1」で始まるすべての名前の詳細が追加されませんでした。配列で。新しいアイデアや提案をありがとうございます。