当前位置: 代码迷 >> J2SE >> 生手提问 java读取txt区分
  详细解决方案

生手提问 java读取txt区分

热度:16   发布时间:2016-04-24 12:23:04.0
新手提问 java读取txt区分
下面是txt文件
<question>
Analyze the following code:
public class Test {
private int t;
public static void main(String[] args) {
int x;
System.out.println(t);
}
}
<choice>
The variable t is not initialized and therefore causes errors
The variable t is private and therefore cannot be accessed in the main method
t is non-static and it cannot be referenced in a static context in the main method.
The variable x is not initialized and therefore causes errors.
The program compiles and runs fine.
<answer>
3


<question>
What is Java (in regard to Computer Science) ?
<choice>
A type of coffee 
An object-oriented programming language 
An interactive website 
none of above
<answer>
2


<question>
Java runs on _______.
<choice>
Windows 
Unix/Linux 
Mac 
All of the Above
<answer>
4

<question>
What is the main function of any variable ?
<choice>
To add numbers together  
To keep track of data in the memory of the computer  
To print words on the screen  
To write Java  
<answer>
2

<question>
The following statements make length be what number ?
int length;
length = 4;
length ++;
<choice>
4  
5  
6  
8  
<answer>
5







下面是代码


import java.io.*;
import java.util.*;

class QuestionLibrary{
private String Question;
private String Answer;
public QuestionLibrary(String _Question,String _Answer){
Question=_Question;
Answer=_Answer;
}
public QuestionLibrary(){};
}
public class read{
private List<QuestionLibrary> list = new ArrayList<QuestionLibrary>();
public void readfile(){
try{  
  File f=new File("Mchoice.txt"); 
   
  BufferedReader bufRead=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
   
  String str; 
  while((str = bufRead.readLine())!= null){
String[] data=new String[100];
data=str.split("<answer>");

QuestionLibrary q = new QuestionLibrary(data[0].trim(),data[1].trim());
list.add(q);
  System.out.println(data[1]); 
  }
  }
  catch (IOException e) {
  System.out.println("Error reading file");
  System.exit(1);
  }
  }

public static void main(String args[])
{ read X=new read();
X.readfile();
}



}

这是代码~~我怎么区分answer 和 question并存到class里

------解决方案--------------------
老实说,楼猪,你是不是马甲来的,跟另一个问题出奇的像,
http://topic.csdn.net/u/20110901/21/d2bf57a7-5eb4-476c-8523-584064b9a618.html?seed=31379635&r=75294767#r_75294767
Java code
import java.io.*;import java.util.*;import java.util.List;class QuestionLibrary {    private static int counter = 1;    public final int id = counter++;// 给每一道question一个唯一的id    private String question;    private List<String> choices;// 每一个问题里都对应有若干个choice,那么把这些choice也存起来吧;    private String answer;    QuestionLibrary(String question, List<String> choices, String answer) {    super();    this.question = question;    this.choices = choices;    this.answer = answer;    }    public String getQuestion() {// 你是用private来修饰question的,那么别的类就只能通过此方法来获取question了    return question;    }    public void setQuestion(String question) {    this.question = question;    }    public List<String> getChoices() {    return choices;    }    public void setChoices(List<String> choices) {    this.choices = choices;    }    public String getAnswer() {    return answer;    }    public void setAnswer(String answer) {    this.answer = answer;    }}public class ReadTxtToClass {     List<QuestionLibrary> list = new ArrayList<QuestionLibrary>();// 保存所有question     BufferedReader br = null;     String line = null;     StringBuffer question = null;     List<String> choice = null;     StringBuffer answer = null;     public void fileToLibrary() throws Exception {    list = new ArrayList<QuestionLibrary>();    br = new BufferedReader(new FileReader("test.txt"));    question = new StringBuffer();    choice = new ArrayList<String>();    answer = new StringBuffer();    while ((line = br.readLine()) != null) {//消除文件开头的空行,直到<question>       while(line.trim().equals("<question>")) {//将文件指针移到题目开头,//从<question>下一行开始处理,在下一个<question>或者文件尾结束处理        while (!(line = br.readLine().trim()).equals("<choice>"))            //读取<choice>前的信息            question.append(line + "\n");        while (!(line = br.readLine().trim()).equals("<answer>"))            //读取<answer>前的信息            choice.add(new String(line));        //每一个替有多个选项,每个question的每一个选项之间必须有明显的界限,但文件中没有给出(某些选项是以“.”号结束,但有些没有)        //这里我假定每一行就是一个选项        while ((line = br.readLine()) != null) {            if (!(line = line.trim()).equals("<question>"))// 如果还未到文件尾,读取下一个<question>前的信息            answer.append(line + "\n");            else break;        }        QuestionLibrary q = new QuestionLibrary(new String(question),            new ArrayList(choice), new String(answer));        list.add(q);                question.delete(0, question.length());//把里面的内容清空,以便存放下一道题        choice.removeAll(choice);        answer.delete(0, answer.length());                if(line == null) break;//如果已经到了文件尾        }       if(line == null) break;//如果已经到了文件尾    }    }    public static void main(String args[]) throws Exception {    ReadTxtToClass rttc = new ReadTxtToClass();    rttc.fileToLibrary();//将文件信息转换为试题库        List list = rttc.list;//准备查看表中的信息    for(int i=0; i< list.size(); i++) {        System.out.println("---------问题" + (i+1)+"\n" +((QuestionLibrary)list.get(i)).getQuestion());        System.out.println("---------问题" + (i+1)+"的选项");                for(String temp : ((QuestionLibrary)list.get(i)).getChoices())        System.out.println(temp);        System.out.println("---------问题" + (i+1)+"的答案\n" +((QuestionLibrary)list.get(i)).getAnswer());        }    }}/*output: ---------问题1Analyze the following code:public class Test {private int t;public static void main(String[] args) {int x;System.out.println(t);}}---------问题1的选项The variable t is not initialized and therefore causes errorsThe variable t is private and therefore cannot be accessed in the main methodt is non-static and it cannot be referenced in a static context in the main method.The variable x is not initialized and therefore causes errors.The program compiles and runs fine.---------问题1的答案3---------问题2What is Java (in regard to Computer Science) ?---------问题2的选项A type of coffeeAn object-oriented programming languageAn interactive websitenone of above---------问题2的答案2---------问题3Java runs on _______.---------问题3的选项WindowsUnix/LinuxMacAll of the Above---------问题3的答案4---------问题4What is the main function of any variable ?---------问题4的选项To add numbers togetherTo keep track of data in the memory of the computerTo print words on the screenTo write Java---------问题4的答案2---------问题5The following statements make length be what number ?int length;length = 4;length ++;---------问题5的选项4568---------问题5的答案5   */
  相关解决方案