I hope I can have some great suggestion how I can solve this here:
I have this text file with places and names - name detail indicates the place(s) that the person has visited:
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
Here is a part of my code.
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());
}
}
My idea is to select all "1." from the text file first and add it in the array, continue with all "2." and add it in the array, and so on.
I have tried many ways, but it did not add all Name detail with the beginning of "1." in the array. I appreciate for any new ideas or suggestions, thanks!