读取格式化文本文件 +提取某些信息+将其加载到 JList 中

发布于 2024-11-06 07:49:30 字数 2054 浏览 2 评论 0原文

所以在这里我得到了可以读取文本文件的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadTextFile {
    public static void main(String[] args) {
        File file = new File("Test.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // show file contents here
        System.out.println(contents.toString());
    }
}

好的,现在我需要我的文本文件(Test.txt)具有以下结构(示例):

topic:- <climate>subtopic1, <atmosphere_type>subtopic2

subtopic1:- <temperatures>sentence1, <gases>sentence2, <winds>sentence3
subtopic2:- <gas_composition>sentence4, <gravitational_weight>sentence5

sentence1:- temperatures are around 34ºC but they can reach -42ºC in winter
sentence2:- there are significant proportions of nitrogen (13%) and butane (24%)
sentence3:- there are permanent winds with gusts of 118 km/h
sentence4:- methane (48%), nitrogen (13%), butane (24%) and oxygen (12%)
sentence5:- gravity in ecuador is 7.95 atmospheres

我真正需要的是有2个JList,在第一个 JList 中,我可以选择一个主题(例如“气候”或“大气类型”),然后在第二个 JList 中,我选择一个子主题(如果我选择“气候”,那么我可以选择“温度”、“气体”或“风”),所以当我点击JButton时,程序会显示相应的句子。做这样的事很难吗?感谢您的帮助! :)

So here I got my code that would read a text file:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadTextFile {
    public static void main(String[] args) {
        File file = new File("Test.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // show file contents here
        System.out.println(contents.toString());
    }
}

Ok, now I need my text file (Test.txt) to have the following structure (Example):

topic:- <climate>subtopic1, <atmosphere_type>subtopic2

subtopic1:- <temperatures>sentence1, <gases>sentence2, <winds>sentence3
subtopic2:- <gas_composition>sentence4, <gravitational_weight>sentence5

sentence1:- temperatures are around 34ºC but they can reach -42ºC in winter
sentence2:- there are significant proportions of nitrogen (13%) and butane (24%)
sentence3:- there are permanent winds with gusts of 118 km/h
sentence4:- methane (48%), nitrogen (13%), butane (24%) and oxygen (12%)
sentence5:- gravity in ecuador is 7.95 atmospheres

What I really need is to have 2 JList, where in the first JList I could choose a topic (Like "climate" or "atmosphere type") and then on my second JList I'd select a subtopic (If I choose "climate", then I could choose "temperatures", "gases" or "winds"), so when I hit a JButton, the program would show me the corresponding sentence. Is it hard to do something like that? Thanks for your help! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2024-11-13 07:49:30

尝试从该文件构建以下数据结构。

// map of a topic (or subtopic) name to a list of subtopic (or sentence) names
Map<String, List<String>> subtopics = new HashMap<String, List<String>>();

// The above will contain the entries:
// topic -> [subtopic1, subtopic2]
// subtopic1 -> [sentence1, sentence2, sentence3]
// subtopic2 -> [sentence4, sentence5]

// map of topic/sentence name to heading
Map<String, String> headings = new HashMap<String, String>();

// This will contain:
// subtopic1 -> climate
// subtopic2 -> atmosphere type
// sentence1 -> temperatures
// sentence2 -> gases
// sentence3 -> winds
// sentence4 -> gas composition
// sentence5 -> gravitational weight

// dictionary for looking up a sentence name and retrieving its corresponding text
Map<String, String> dict = new HashMap<String, String>();

// sentence1 -> temperatures are around 34ºC but they can reach -42ºC in winter
// sentence2 -> there are significant proportions of nitrogen (13%) and butane (24%)
// sentence3 -> there are permanent winds with gusts of 118 km/h
// sentence4 -> methane (48%), nitrogen (13%), butane (24%) and oxygen (12%)
// sentence5 -> gravity in ecuador is 7.95 atmospheres

这应该会让你朝着好的方向前进。需要注意的一点是,当第一次将主题添加到“子主题”映射时,您必须记住首先创建列表(例如 ArrayList),将子主题名称添加到列表中,并将列表作为值为主题名称。如果在列表中找到主题名称,只需将子主题名称添加到现有列表中即可。

Try to build the following data structures from the file.

// map of a topic (or subtopic) name to a list of subtopic (or sentence) names
Map<String, List<String>> subtopics = new HashMap<String, List<String>>();

// The above will contain the entries:
// topic -> [subtopic1, subtopic2]
// subtopic1 -> [sentence1, sentence2, sentence3]
// subtopic2 -> [sentence4, sentence5]

// map of topic/sentence name to heading
Map<String, String> headings = new HashMap<String, String>();

// This will contain:
// subtopic1 -> climate
// subtopic2 -> atmosphere type
// sentence1 -> temperatures
// sentence2 -> gases
// sentence3 -> winds
// sentence4 -> gas composition
// sentence5 -> gravitational weight

// dictionary for looking up a sentence name and retrieving its corresponding text
Map<String, String> dict = new HashMap<String, String>();

// sentence1 -> temperatures are around 34ºC but they can reach -42ºC in winter
// sentence2 -> there are significant proportions of nitrogen (13%) and butane (24%)
// sentence3 -> there are permanent winds with gusts of 118 km/h
// sentence4 -> methane (48%), nitrogen (13%), butane (24%) and oxygen (12%)
// sentence5 -> gravity in ecuador is 7.95 atmospheres

This should get you going in a good direction. One bit to watch for is when adding a topic to the 'subtopics' map for the first time, you have to remember to first create the List (e.g. an ArrayList), add the subtopic name to the list and and the list as the value for the topic name. If the topic name is found in the list, just add the subtopic name to the existing List.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文