Java 使用while循环读取文件
我正在为学校做一个项目,我在这里遇到了一些麻烦。我要翻阅一份档案,准备做一个琐事游戏。文件的格式如下:文件的第一行是问题的类别,下面几行是成对的问题和答案。直到它命中一条空行,这表示空白行后的下一行开始新的类别,然后继续。我应该制作一个有6个索引的ArrayList。每个类别一个,然后在每个索引中,我应该有一组问题和答案,我可以利用这些问题和答案进行比较。我猜基本上是arraylist中的一个数组。 我希望我努力实现的目标是有意义的。这让我很困惑。但这里是我试图工作的代码,有这么多的部分,我变得非常混乱Java 使用while循环读取文件,java,file-io,arraylist,while-loop,Java,File Io,Arraylist,While Loop,我正在为学校做一个项目,我在这里遇到了一些麻烦。我要翻阅一份档案,准备做一个琐事游戏。文件的格式如下:文件的第一行是问题的类别,下面几行是成对的问题和答案。直到它命中一条空行,这表示空白行后的下一行开始新的类别,然后继续。我应该制作一个有6个索引的ArrayList。每个类别一个,然后在每个索引中,我应该有一组问题和答案,我可以利用这些问题和答案进行比较。我猜基本上是arraylist中的一个数组。 我希望我努力实现的目标是有意义的。这让我很困惑。但这里是我试图工作的代码,有这么多的部分,我变得
import java.util.ArrayList;
public class TriviaQuestion {
private String player;
private String category;
private String question;
private String answer;
private int score = 0;
/**
*
*/
public TriviaQuestion() {
player = "unknown";
category = "unknown";
question = "unknown";
answer = "unknown";
score = 0;
}
public TriviaQuestion(String category, String question, String answer){
}
public TriviaQuestion(String question, String answer){
}
public TriviaQuestion(String player, String category, String question,
String answer, int score) {
super();
this.player = player;
this.category = category;
this.question = question;
this.answer = answer;
this.score = score;
}
/**
* @return the player
*/
public String getPlayer() {
return player;
}
/**
* @param player the player to set
*/
public void setPlayer(String player) {
this.player = player;
}
/**
* @return the category
*/
public String getCategory() {
return category;
}
/**
* @param category the category to set
*/
public void setCategory(String category) {
this.category = category;
}
/**
* @return the question
*/
public String getQuestion() {
return question;
}
/**
* @param question the question to set
*/
public void setQuestion(String question) {
this.question = question;
}
/**
* @return the answer
*/
public String getAnswer() {
return answer;
}
/**
* @param answer the answer to set
*/
public void setAnswer(String answer) {
this.answer = answer;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TriviaQuestion [category=" + category + ", question="
+ question + ", answer=" + answer + "]";
}
}
然后是测试人员,我试着在课堂最底层做一些笔记,但在这一点上,它们对我来说没有多大意义
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class TriviaQuestion2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File gameFile = new File("trivia.txt");
Scanner inFile = new Scanner(gameFile);
ArrayList<TriviaQuestion> question = new ArrayList<TriviaQuestion> ();
while (inFile.hasNextLine()){
boolean cat = inFile.hasNextLine();
boolean more = true;
while (inFile.hasNextLine() && more);
String answer;
TriviaQuestion temp = new TriviaQuestion(question, answer);
question[0] = new ArrayList<TriviaQuestion>();
new TriviaQuestion(q,a);
question[0].add(temp);
}
}
}
inFile.close();
//use a while loop inside a while loop, and first do category, then question/answer
//there are six categories, use array of trivia questions ArrayList <triviaquestions>[]
//question = new ArrayList<triviaQuesitons>[6]
//question[0] = new ArrayList<triviaQuesitons>();
/**
* while in question[0] and so on read the quesiton and answer
* temp = new tq(question, answer)
* question [0].add(temp)
* question[0].get(i)
*
*/
System.out.println(question);
}
// TODO Auto-generated method stub
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Scanner;
公共课琐事问题2{
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException{
File gameFile=新文件(“trivia.txt”);
扫描仪内嵌=新扫描仪(游戏文件);
ArrayList问题=新的ArrayList();
while(infle.hasNextLine()){
布尔cat=infle.hasNextLine();
布尔更多=真;
while(infle.hasNextLine()&更多);
字符串回答;
TriviaQuestion temp=新的TriviaQuestion(问题,答案);
问题[0]=新建ArrayList();
新的琐事问题(q,a);
问题[0]。添加(临时);
}
}
}
infle.close();
//在while循环中使用while循环,首先进行分类,然后提问/回答
//共有六类,使用琐事问题数组ArrayList[]
//问题=新阵列列表[6]
//问题[0]=新建ArrayList();
/**
*在问题[0]等中,阅读问题并回答
*临时=新tq(问题、答案)
*问题[0]。添加(临时)
*问题[0]。获取(i)
*
*/
System.out.println(问题);
}
//TODO自动生成的方法存根
}
您需要一些嵌套循环来读取文件
这是一种描述文件格式的糟糕方法。“节”描述文件的一节。可以说,每个部分定义了一个类别,第一行是类别的名称,第二行是成对的问题和答案,最后是一个空行/或EOF
在伪代码中,基本上需要:
List<Category> categories = new ArrayList();
while (in.hasNextLine()) {
String catName = in.readLine();
if (catName.trim().length() == 0)
continue; // skip extra blank lines between Sections.
Category cat = new Category( catName);
categories.add( cat);
while (in.hasNextLine()) {
String line = in.readLine();
if (line.trim().length() == 0)
break; // end of Section.
// parse a Question & it's Answer.
TriviaQuestion question = parseQuestion( line);
cat.addQuestion( question);
}
}
// done.
return categories;
List categories=new ArrayList();
while(在.hasNextLine()中){
字符串catName=in.readLine();
if(catName.trim().length()==0)
continue;//跳过节之间的额外空行。
类别类别=新类别(类别名称);
类别。添加(cat);
while(在.hasNextLine()中){
String line=in.readLine();
如果(line.trim().length()==0)
break;//节的结尾。
//解析一个问题&它的答案。
TriviaQuestion=parseQuestion(行);
第二类问题(问题);
}
}
//完成了。
退货类别;
我应该制作一个有6个索引的ArrayList。一个
每个类别,然后在每个索引中,我应该有一组
我将能够用于比较的问题和答案
这是一种相当混乱的方式来谈论一些实际上应该非常简单的事情。你可以说“将有6个类别”。但实际上,没有必要固定或预先确定ArrayList的大小——所以为什么要含糊其辞呢
我猜基本上是arraylist中的一个数组
在ArrayList内部(间接)使用ArrayList。列表/数组列表更适合动态构建,因为(与数组不同)它们不必预先调整大小或增长
但请注意,“问题”列表应保存在obect类别内。您没有解释您的错误,这不是调试服务。调试您的代码,找出不符合预期的内容,然后返回发布适当的问题。除此之外,您的文件格式很复杂,并且容易失败。我建议搬到更好的地方去;也许一个
Properties
文件可以myCategory.question1=
。如果你觉得你能处理它,XML或DB。好吧,就是这样。我的知识和我所能做的工作都非常有限。我不能只使用映射arraylist和while循环来处理它。这段代码应该获取我的文本文件,为6个琐事类别创建一个arraylist,然后将问答部分存储在arraylist索引内的数组中。我得到的错误是eclipse不喜欢我上面的任何东西。谢谢你的帮助。我对编程还是相当陌生,所以我很抱歉我对术语缺乏理解。