我正在尝试创建一个描述npc的NPC类。单个npc具有ID并具有名称。如何将名称列表中的名称和ID放入ID中。
我从文本文件中分隔了名称和ID。 m.group()是id,m2.group()是名称。基本上我不明白的是如何将组添加到列表中。我尝试使用
getNPC().npcs.add(m.group());
但不起作用?
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile {
private String path;
public NPC npc;
public NPC getNPC() {
return npc;
}
public ReadFile(String file_path) {
this.path = file_path;
}
public String[] openFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for (int i=0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null) {
numberOfLines++;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(aLine);
int id = m.group().indexOf(m.group().length());
Pattern p2 = Pattern.compile("[a-z\\sA-Z]+$");
Matcher m2 = p2.matcher(aLine);
String name = m2.group();
while (m.find() && m2.find()) {
System.out.println(m.group());
System.out.println(m2.group());
getNPC().npcs.add(new NPC(id, name));
}
}
bf.close();
return numberOfLines;
}
}
这是我用来从文本文件中分离IDS和名称的类。
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile {
private String path;
public NPC npc;
public NPC getNPC() {
return npc;
}
public ReadFile(String file_path) {
this.path = file_path;
}
public String[] openFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for (int i=0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null) {
numberOfLines++;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(aLine);
int id = m.group().indexOf(m.group().length());
Pattern p2 = Pattern.compile("[a-z\\sA-Z]+$");
Matcher m2 = p2.matcher(aLine);
String name = m2.group();
while (m.find() && m2.find()) {
System.out.println(m.group());
System.out.println(m2.group());
getNPC().npcs.add(new NPC(id, name));
}
}
bf.close();
return numberOfLines;
}
}
答案 0 :(得分:0)
如果你真的想拥有单独的id和名称数组,那么你需要有两个ArrayList。
ArrayList<String> npcIds;
ArrayList<String> npcNames;
然后你应该可以做.add()。
更好的解决方案可能是具有npcs
的ArrayListpublic ArrayList<NPC> npcs;
然后你会像这样添加它们
getNPC().npcs.add(new NPC(Integer.parsInt(m.group()), m2.group()));
像@ jon-diaet建议的那样