当前位置: 首页 > 面试题库 >

仅将文件中的特定文本添加到数组列表,第2部分-Java

邓建柏
2023-03-14
问题内容

我希望我能提出一些很好的建议,以解决这个问题:

我有一个带有位置和名称的文本文件-名称详细信息指示此人访问过的位置:

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


问题答案:

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

这是对代码的一种经过测试的重新创建,因为您引用了一些您未提供的类…但这有望对您有所帮助:

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


 类似资料:
  • 问题内容: 我需要能够向ELF文件添加任意部分。我无法在该程序中使用GPL代码,因此BFD成为不可能。我可以使用libelf / gelf来阅读节,但是这些文档很少,因此我无法弄清楚如何添加节。有人知道怎么做这个吗?我宁愿不编写自己的ELF代码。 问题答案: 关于ELF文件标头的问题有几个(可能)相关答案。提到的接受的答案用于将部分添加到ELF文件,并且BSD bintools 声称具有BSD许可

  • 我正试图找出如何从如下格式的文本文件中读取数据: 1.0,8.0 4.0,3.0 6.0,0.0 3.0,5.0 在myNumbers1中添加每行的第一个元素,在myNumbers2中添加第二个元素,如下所示: double[]myNumbers1={} double[]myNumbers2={}; 要得到 double[]myNumbers1={1.0,4.0,6.0,3.0}<双[]myNum

  • 我正在为一个网格窗格分配几个按钮。每个按钮都有一个数字作为其文本。我创建了一个arrayList numbers并向其添加数字。我使用集合来洗牌数字(我希望每次运行程序时在每个按钮中都有一个随机数)。然后我使用这个ArrayList向每个按钮文本添加一个数字。 我希望每个按钮都有不同的编号。但是,现在1列中的每个按钮都有相同的编号。 任何关于如何修复此问题的建议都将非常感谢(按钮的文本随机分配)。

  • 在有趣的功能中,当我将列表添加到其他列表时,它正在添加空列表,我可以找到原因有人可以帮助我这个程序是关于查找给定数组的不同组合

  • 问题内容: 在我的在线计算机科学课上,我必须编写一个程序来确定太阳系中每个行星的表面重力。除了一个方面,我几乎已经掌握了它的所有方面。我需要使用单独的方法将表面重力写入文件。这是我目前的方法: 我的问题是,当我将其写入文件时,它将覆盖先前的值。我如何获得它包括所有的价值。如果有帮助,这是我的全部代码: 问题答案: 这样做是为了创建带有追加模式的作品:

  • 问题内容: 我想补充一个特定的线路,可以说,到开头的文件,并应添加到该文件中的15号线。 这是向文件添加文本的方法: 这是如何向以MakeFile开头的文件中添加文本,我认为: 但是我无法将此行添加到文件的第15行。 问题答案: 您可以用来解决此问题: 或使用选项保存对文件所做的更改。 要更改所有从此开始的文件: 注意:以上是放置文本的兴趣点。