java - 如何捕获 2 个元素并合并它们?
问题描述
我希望能够从我的文本文件中捕获每个房间的描述并存储整个描述,而不管它在一个元素中有多少行。
堆栈溢出也不允许我显示我的文本文件实际上是如何格式化的,它实际上是这样的:
编号 = $
房间=
描述:
退出:
但没有空行分隔它们
代码:
package MiniTextBasedGame;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class fileParser {
public static String[] getLines(File file){
String contents = null;
Path path = file.toPath();
String[] lines = null;
try {
contents = Files.readString(path, StandardCharsets.ISO_8859_1);
} catch (IOException ex) {
System.out.println("Error");
}
lines = contents.split("\n");
return lines;
}
public static void getRoomID(String[] array){
ArrayList<String> arraylist= new ArrayList<>(Arrays.asList(array));
ArrayList<String> idarraylist = new ArrayList<>();
for (String str: arraylist) {
if(str.contains("ID")){
String temp = str.substring(5, 6);
idarraylist.add(temp);
}
}
}
public static void getRoomName(String[] array){
ArrayList<String> arraylist= new ArrayList<>(Arrays.asList(array));
ArrayList<String> namearraylist = new ArrayList<>();
for (String str: arraylist) {
if(str.contains("name =")){
String temp = str.substring(7);
namearraylist.add(temp);
}
}
}
public static void getRoomDescription(String[] array){
ArrayList<String> darray = new ArrayList<>(Arrays.asList(array));
ArrayList<String> tempArray = new ArrayList<>();
for (String str: darray) {
if (!(str.contains("ID") || str.contains("name =") || str.contains("Description:") || str.contains("exits"))) {
if (!str.isBlank()) {
tempArray.add(str);
}
}
}
// Print out results
for (String str: tempArray) {
System.out.println(str);
}
}
public static void getExits(String[] array){
}
}
文本文件:
ID = 0
name = Beginning
Description: Y0ou are standing at the end of a road before a dark and dank cave. The forest is
covering the road lending a dark and spooky aspect to the road. You hear a low wailing that
seems to be coming from the cave.
exits : East
ID = 1
name = Room1
Description: Y1ou are standing at the end of a
road before a dark and dank cave. The forest is covering the road
lending a dark and spooky aspect to the road. You hear a low wailing
that seems to be coming from the cave. exits : East, West, South
ID = 2
name = Room2
Description: Y2ou are standing at the end of a
road before a dark and dank cave. The forest is covering the road
lending a dark and spooky aspect to the road. You hear a low wailing
that seems to be coming from the cave.
exits : South, West
ID = 3
name = Room3
Description: Y3ou are standing at the end of a
road before a dark and dank cave. The forest is covering the road
lending a dark and spooky aspect to the road. You hear a low wailing
that seems to be coming from the cave.
exits : North, West
ID = 4
name = Room4
Description: Y4ou are standing at the end of a
road before a dark and dank cave. The forest is covering the road
lending a dark and spooky aspect to the road. You hear a low wailing
that seems to be coming from the cave.
exits : East, North, South
ID = 5
name = End
Description: Y5ou are standing at the end of a road
before a dark and dank cave. The forest is covering the road lending a
dark and spooky aspect to the road. You hear a low wailing that seems
to be coming from the cave.
exits : North
解决方案
你最好使用正则表达式来解析你的文件。这是一个可以从文件中提取信息的示例正则表达式:
ID = ([0-9]+)\s*name = (.*)\s*Description: ([\s\S]+?)exits : (.*)
在 Java 中使用它:
String str = Files.readString(path, StandardCharsets.ISO_8859_1);
Pattern p = Pattern.compile("ID = ([0-9]+)\\s*name = (.*)\\s*Description: ([\\s\\S]+?)exits : (.*)");
Matcher m = p.matcher(str);
while(m.find())
{
String id = m.group(1).trim();
String name = m.group(2).trim();
String description = m.group(3).trim();
String exits = m.group(4).trim();
//do the rest...
}
推荐阅读
- c++ - 在不使用朋友类的情况下访问私有成员
- javascript - 前一行代码完成后如何使一行代码工作?
- java - 如何以编程方式更改 Google 键盘外观?
- bash - AWS Session Manager 没有采购 bash rc
- c# - LoadedBehavior="Manual" 时 WPF MediaElement 不显示视频
- java - 如何使用 SpringBootTest 启用 JPA 审计?
- python-3.x - 从不同目录的不同脚本导入变量时出错
- python - Python最小样板类初始化
- javascript - Vue.js 类未实例化
- javascript - node.js Googe 身份验证回调错误