Adding only specific text from a file to an array

2019-03-06 16:38发布

问题:

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!

回答1:

It might be better to use a regular expression to extract the digit on the line instead of trying to track/guess it (see below).

This is a tested recreation of your code since you reference some classes that you didn't provide...but this should hopefully help:

public static String getMatch(final String pattern, final String content) {
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(content);

    if (m.find()) {
        return m.group(1);
    } else {
        return "";
    }
}

public static void load(String fileName) throws FileNotFoundException {

    List<String> places = new ArrayList<String>();
    List<String> names = new ArrayList<String>();
    List<String> nameDetails = new ArrayList<String>();

    BufferedReader br = new BufferedReader(new FileReader(fileName));

    String text;
    String lastName = "";

    try {

        while ((text = br.readLine()) != null) {
            // extract num from start of line or empty if none..
            String num = getMatch("^([0-9]+)\\.", text);

            if (text.contains("Place:")) {
                text = text.replaceAll("Place:", "");
                places.add(text);
            } else if (text.contains(num + ". Name:")) {
                text = text.replaceAll(num + ". Name:", "");
                names.add(text);
                lastName = text;
            } else if (text.contains(num + ". Name detail:")) {
                text = text.replaceAll(num + ". Name detail:", "");
                nameDetails.add(lastName + " had " + text);
            }
        }
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }

    System.out.println("Places:" + places);
    System.out.println("Names:" + names);
    System.out.println("Name Details:" + nameDetails);
}


回答2:

Instead of having a Name class, how about a Traveler class that has String name and List<Place> places? You could then have a method add(Place p) on the Traveler class.