-1

我希望我能有一些很好的建议,我可以在这里解决这个问题:

我有这个包含地点和名称的文本文件 - 名称详细信息表示此人访问过的地点:

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”开头的所有名称详细信息。在数组中。我感谢任何新的想法或建议,谢谢!

4

2 回答 2

0

与其拥有 Name 类,不如使用具有String nameand的 Traveler 类List<Place> places?然后,您可以add(Place p)在 Traveler 类上有一个方法。

于 2013-04-18T19:46:24.297 回答
0

最好使用正则表达式来提取线上的数字,而不是尝试跟踪/猜测它(见下文)。

这是您的代码的经过测试的重新创建,因为您引用了一些您没有提供的类......但这应该会有所帮助:

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);
}
于 2013-04-19T05:11:44.587 回答