我目前正在尝试编写自己的Magic Item Generator供个人使用。现在,我坚持尝试将一堆拼写和对象描述加载到数组中,如果将拼写/对象滚动到魔法项创建中,将从数组中调用这些数组。
我目前无法尝试将多个描述加载到数组中。我只能在退出之前将一个描述放入数组中。
目前我有这段代码:
public class LoadDescription {
public static ArrayList<String> descriptions = new ArrayList<String>();
public static void main(String[] args) throws IOException {
File filename = new File("spellconcept.txt");
loadDescriptions(filename);
System.out.println(descriptions.get(0));
// System.out.println(descriptions.get(1));
}
public static void loadDescriptions(File name) throws IOException {
FileReader fr = new FileReader(name);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
int i = 0;
while (!br.readLine().equals("@@@")) {
try {
String line = br.readLine();
while (!line.isEmpty()) {
sb.append(" " + line);
line = br.readLine();
}
} catch (IOException e) {
}
;
descriptions.add(sb.toString());
}
i++;
}}
这是我尝试使用的文本文件。请忽略它中缺乏智能,它只是一个测试文件:
@This is a description of a spell.
I often wonder how often I can write
the word often.Without seeming that it is too often that I write this out.
Does it not seem weird. The quick brown fox jumps over the small red fence.
@This is another description of a spell
@Maybe add another line here, \n see if this works? maybe?
@@@
答案 0 :(得分:0)
在循环的顶部,
while (!br.readLine().equals("@@@")) {
你打电话给readLine()
,消耗1行线,但你不能捕捉线本身来处理它。
答案 1 :(得分:0)
以下是修改后的代码:
@这是一个咒语的描述。
我经常想知道我经常能够经常写这个词。但是我似乎经常把它写出来。 这看起来不奇怪吗?快速的棕色狐狸跳过小红篱笆。
@这是对法术的另一种描述
苹果是水果。 它是红色的。@ 3rd description
我有一只宠物。 它是一只名叫约翰的狗。
@@@
public static ArrayList<String> descriptions = new ArrayList<String>();
public static void main(String[] args) throws IOException {
File filename = new File("C:\\temp\\test.txt");
loadDescriptions(filename);
System.out.println("*************");
for(String ln:descriptions){
System.out.println(ln);
}
}
public static void loadDescriptions(File name) throws IOException {
FileReader fr = new FileReader(name);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
int i = 0;
String line=null;
while ((line = br.readLine()) != null ) {
if(line.startsWith("@")){
if(i>0){
descriptions.add(sb.toString());
sb = new StringBuilder();
}
}else{
if(!line.isEmpty()){
System.out.println(line);
sb.append(" " + line);
}
}
i++;
}
}