仅将文件中的特定文本添加到数组列表,第 2 部分 - Java [英] Adding only specific text from a file to an array list, part 2 - Java

查看:18
本文介绍了仅将文件中的特定文本添加到数组列表,第 2 部分 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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());
    }
}

我的想法是全选1".首先从文本文件中将其添加到数组中,继续所有2".并将其添加到数组中,依此类推.

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.

我尝试了很多方法,但它没有添加所有以1"开头的名称细节.在数组中.感谢您提供任何新的想法或建议,谢谢!

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!

推荐答案

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

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 部分 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆