【问题标题】:Assigning properties to an object in Java from a text file从文本文件将属性分配给 Java 中的对象
【发布时间】:2017-05-19 15:54:35
【问题描述】:

我目前正在构建一个基于游戏书文本的冒险程序,它将接受某种格式的 txt 文件,如下所示。

现在这一切看起来都很简单,但是我需要创建 Section 对象才能让我的游戏正常运行。正如您在上面看到的,这些部分的大小通常不同,具体取决于他们对相应故事文本有多少可能的选择。我的部分对象有

的声明
Section(String storytext, String choiceText, int choiceSections)

我需要从这个文本文件中提取适当的细节。我正在阅读一些论坛,并找到了一种比较合适的方法来阅读文本文件。代码如下,适用于三元组,对我来说不一定好,因为我的文本文件长度可变。显然这只是一个代码示例,我需要解析出整数和字符串。

Car[] car = new Car[length/3];

for (int i = 0; i < length/3; i ++) // 
{
    int startReading = Integer.parseInt(inputFile.readLine());
    int endReading = Integer.parseInt(inputFile.readLine());
    int liter = Integer.parseInt(inputFile.readLine());
    car[i] = new Car (startKm, endKm, litre);
}

这似乎对我有用,但是我需要我的循环根据一些标准动态更新。 #1 > 第一行的数字是要创建的部分对象的总数。 #2 > 总选择行上的数字,它将决定行阅读器在停止和创建对象之前还需要运行多少行。我对如何创建一个适当的循环非常迷茫,该循环将根据这些文本文件的这个标准创建我的对象。

6

1

一天晚上,在制作花生酱和果酱三明治时,您发现草莓酱用完了。

2

5

如果您使用葡萄果冻,请单击此处。

4

如果您要去商店购买草莓酱,请单击此处。

2

您回到家,开始用您最喜欢的调味品制作新鲜的三明治。你慈爱地撒上花生酱和草莓酱,美妙的香气让人垂涎三尺。再也忍不住,咬了一口。极致的幸福笼罩着你,完美的混合风味,令人敬畏的瀑布充满你的意识。

0

3

你的胃立即开始疼痛。疼痛刺痛你的腹部,你翻了个身,大口喘气。地板崩塌,黑暗笼罩着你的视线。

0

4

你冲到杂货店寻找你最喜欢的草莓酱品牌。你很惊讶地在熟悉的罐子旁边的架子上发现了一种新品种野地莓。

2

2

如果您购买草莓酱,请单击此处。

6

如果您购买新的野莓果酱,请单击此处。

5

你在冰箱里翻了一会儿,发现你记得的葡萄果冻在那儿。它闻起来有点时髦,但你还是把它涂在面包上,屏住呼吸。

2

3

如果你吃了发臭的三明治,请点击这里。

4

如果你把它扔进垃圾桶然后出去买果酱,请点击这里。

6

您回到家,开始用令人兴奋的新三明治制作新鲜三明治 调味品。你热情地撒上花生酱和野莓果酱,美妙的香气让人垂涎三尺。再也忍不住,咬了一口。

1

3

【问题讨论】:

  • 这是一个合理的问题 - 只是描述有点太长了
  • 是的,我只是稍微改变了格式,将我的问题放在顶部,然后是文本文件,希望能引起人们的注意。这是我希望得到帮助的原因,因为我似乎无法使用缓冲读取器或像这样的文件读取器找到任何东西。

标签: java loops object text reader


【解决方案1】:

这样就可以了:

public class Main {

    public static void main(String[] args) throws IOException {
         try (BufferedReader br = new BufferedReader(new FileReader(
                "C:\\path\\to\\file"))) {
            String line = br.readLine().trim(); // may throw IOException
            int num_of_sections = Integer.parseInt(line);
            List<Section> sections = new ArrayList<>(num_of_sections);
            for (int j = 1; j <= num_of_sections; ++j) {
                line = _skip_empty(br);
                int section_number = Integer.parseInt(line);
                String section_text = _skip_empty(br);
                line = _skip_empty(br);
                int num_choices = Integer.parseInt(line);
                List<Choice> choices = new ArrayList<>(num_choices);
                for (int choice = 0; choice < num_choices; ++choice) {
                    line = _skip_empty(br);
                    int go_to_section = Integer.parseInt(line);
                    String choice_text = _skip_empty(br);
                    choices.add(new Choice(go_to_section, choice_text));
                }
                sections.add(new Section(section_number, section_text, choices));
            }
        }
    }

    private static class Section {
        private final int sectionNumber;
        private final String storyText;
        private final List<Choice> choices;

        Section(int sectionNumber, String storyText, List<Choice> choices) {
            this.sectionNumber = sectionNumber;
            this.storyText = storyText;
            this.choices = choices;
        }

        public String getStoryText() {
            return storyText;
        }

        public List<Choice> getChoices() {
            return choices;
        }

        public int getSectionNumber() {
            return sectionNumber;
        }
    }

    private static class Choice {
        private final int leadsToSection;
        private final String choiceText;

        Choice(int leadsToSection, String choiceText) {
            this.leadsToSection = leadsToSection;
            this.choiceText = choiceText;
        }

        public int getLeadsToSection() {
            return leadsToSection;
        }

        public String getChoiceText() {
            return choiceText;
        }
    }

    private static String _skip_empty(BufferedReader br) throws IOException {
        String line;
        do {
            line = br.readLine();
            if (line == null) return ""; // winning choice does that
            line = line.trim();
        } while (line.isEmpty());
        return line;
    }
}

如您所见,我更改了您的 Section 类以接受选择列表 - 这更合适

【讨论】:

  • 这正是我一直在寻找的,我对将文件合并到我的程序中真的很陌生,所以你决定使用缓冲阅读器的方式更有意义,谢谢。
  • 大部分是的,我了解您的所有实现以及您如何决定在文本文件中移动,这是天才。还创建跳过文件中的空格或仅读取行的方法可以节省大量时间。但是,当我在运行该方法后打印我的数组列表(例如部分)时,它会完全打印为空值吗?我知道文件路径是正确的,因为我将打印行放在循环的每个部分中,它实际上会将整个文件读取给我。
  • 它对我来说正常打印 - 请检查您是否使用了完全相同的代码。我想我给了你足够的代码来从这里接受它;) - 当然你需要toString 方法等。而且文件格式必须与问题完全相同
猜你喜欢
  • 1970-01-01
  • 2014-04-05
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-29
  • 2020-05-22
  • 1970-01-01
相关资源
最近更新 更多