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

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

问题描述

我希望我能有一些很棒的建议,我怎么能解决这个问题在这里:

I hope I can have some great suggestion how I can solve this here:

我有地方和名字该文本文件 - 名称详细说明该人已到访的地点(S):

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

下面是我的code的一部分。

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的开头添加的所有名称细节在该阵列。我AP preciate任何新的想法或建议,谢谢!

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!

推荐答案

这可能是更好地使用常规的前pression提取数字就行了,而不是试图跟踪/猜它(见下文)。

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).

这是您的code的测试,休闲,因为您引用一些类,你没有提供...但是这应该希望帮助:

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天全站免登陆