从文本文件为 Java 中的对象分配属性
Assigning properties to an object in Java from a text file
我目前正在构建一个基于游戏书文本的冒险程序,它将接受某种格式的 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 > 总选择行上的数字将决定行 reader 在停止和创建对象之前还需要 运行 多少行。我完全不知道如何创建一个适当的循环,该循环将根据这些文本文件的此标准创建我的对象。
6 < "sections" 在游戏中的数量
1 < 节号 (1)
一天晚上,在制作花生酱和果酱三明治时,您发现 运行 没有草莓酱了。 <与该部分相关的故事文本
2 < 可能的选择数
5 < 下面的选择会跳转到此部分
如果你凑合着吃葡萄果冻,点击这里。 <这是选择之一
4 < 下面的选择会跳转到此部分
如果您去商店购买草莓酱,请单击此处。 <这是选择之一
2 < 节号 (2)
你回到家,开始用你最喜欢的调味品做一个新鲜的三明治。你亲切地涂上花生酱和草莓酱,闻到美妙的香气,垂涎三尺。再也忍不住,咬了一口。极乐笼罩着您,各种口味的完美融合让您的意识充满了无穷无尽的美妙体验。 <与此部分相关的故事文本
0 < 可能的选择数
3 < 节号 (3)
您的胃立即开始疼痛。腹部剧痛,你弯下腰,喘着粗气。地板轰然倒塌,黑暗笼罩了你的视线。 <与此部分相关的故事文本
0 < 可能的选择数
4 < 节号 (4)
您冲到杂货店寻找您最喜欢的草莓酱品牌。您惊讶地发现在熟悉的罐子旁边的架子上有一个新品种野莓。 <与此部分相关的故事文本
2 < 可能的选择数
2 < 下面的选择会跳转到此部分
如果您购买草莓酱,请单击此处。 <这是选择之一
6 < 下面的选择会跳转到这一部分
如果您购买新的野莓果酱,请单击此处。 <这是选择之一
5 < 节号 (5)
你在冰箱里翻找了一会儿,发现你记得的葡萄果冻就在那里。它闻起来有点怪,但你还是把它涂在面包上,屏住呼吸。 <与此部分相关的故事文本
2 < 可能的选择数
3 < 下面的选择会跳转到此部分
如果你吃了臭三明治,请点击这里。 <这是选择之一
4 < 下面的选择会跳转到此部分
如果你把它扔进垃圾桶出去买果酱,请点击这里。 <这是选择之一
6 < 节号 (6)
你回到家,开始用你激动人心的新事物制作新鲜的三明治
调味品。你亲切地涂上花生酱和野莓果酱,闻到美妙的香气,垂涎三尺。再也忍不住,咬了一口。 <与此部分相关的故事文本
1 < 可能的选择数
//这里还不知道选什么
3 < 选择部分你也被引导,3 代表获胜所以到此结束。
这样做:
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;
}
}
如您所见,我更改了您的部分 class 以接受选项列表 - 这更合适
我目前正在构建一个基于游戏书文本的冒险程序,它将接受某种格式的 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 > 总选择行上的数字将决定行 reader 在停止和创建对象之前还需要 运行 多少行。我完全不知道如何创建一个适当的循环,该循环将根据这些文本文件的此标准创建我的对象。
6 < "sections" 在游戏中的数量
1 < 节号 (1)
一天晚上,在制作花生酱和果酱三明治时,您发现 运行 没有草莓酱了。 <与该部分相关的故事文本
2 < 可能的选择数
5 < 下面的选择会跳转到此部分
如果你凑合着吃葡萄果冻,点击这里。 <这是选择之一
4 < 下面的选择会跳转到此部分
如果您去商店购买草莓酱,请单击此处。 <这是选择之一
2 < 节号 (2)
你回到家,开始用你最喜欢的调味品做一个新鲜的三明治。你亲切地涂上花生酱和草莓酱,闻到美妙的香气,垂涎三尺。再也忍不住,咬了一口。极乐笼罩着您,各种口味的完美融合让您的意识充满了无穷无尽的美妙体验。 <与此部分相关的故事文本
0 < 可能的选择数
3 < 节号 (3)
您的胃立即开始疼痛。腹部剧痛,你弯下腰,喘着粗气。地板轰然倒塌,黑暗笼罩了你的视线。 <与此部分相关的故事文本
0 < 可能的选择数
4 < 节号 (4)
您冲到杂货店寻找您最喜欢的草莓酱品牌。您惊讶地发现在熟悉的罐子旁边的架子上有一个新品种野莓。 <与此部分相关的故事文本
2 < 可能的选择数
2 < 下面的选择会跳转到此部分
如果您购买草莓酱,请单击此处。 <这是选择之一
6 < 下面的选择会跳转到这一部分
如果您购买新的野莓果酱,请单击此处。 <这是选择之一
5 < 节号 (5)
你在冰箱里翻找了一会儿,发现你记得的葡萄果冻就在那里。它闻起来有点怪,但你还是把它涂在面包上,屏住呼吸。 <与此部分相关的故事文本
2 < 可能的选择数
3 < 下面的选择会跳转到此部分
如果你吃了臭三明治,请点击这里。 <这是选择之一
4 < 下面的选择会跳转到此部分
如果你把它扔进垃圾桶出去买果酱,请点击这里。 <这是选择之一
6 < 节号 (6)
你回到家,开始用你激动人心的新事物制作新鲜的三明治 调味品。你亲切地涂上花生酱和野莓果酱,闻到美妙的香气,垂涎三尺。再也忍不住,咬了一口。 <与此部分相关的故事文本
1 < 可能的选择数 //这里还不知道选什么
3 < 选择部分你也被引导,3 代表获胜所以到此结束。
这样做:
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;
}
}
如您所见,我更改了您的部分 class 以接受选项列表 - 这更合适