如何在Java中循环文本文件,将7行对象添加到对象数组?

时间:2017-07-30 22:35:15

标签: java arraylist file-io

我正在开发一个将成为琐事游戏的项目。我需要创建一个循环文本文件并创建一个对象然后将其添加到ArrayList的方法。该文件的每7行都是一个新对象。对象本身是一个包含诸如可能答案等内容的问题。

我的对象构造函数:

<script>
function loadXMLDoc(filename)
{
if (window.ActiveXObject)
  {
  xhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
else 
  {
  xhttp = new XMLHttpRequest();
  }
xhttp.open("GET", filename, false);
try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}

function displayResult()
{
xml = loadXMLDoc("/data/xml/project_detail/project_detail.xml");
xsl = loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
  {
  ex = xml.transformNode(xsl);
  document.getElementById("example").innerHTML = ex;
  }
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xsltProcessor = new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);
  resultDocument = xsltProcessor.transformToFragment(xml, document);
  document.getElementById("example").appendChild(resultDocument);
  }
}
</script>

我需要遍历一个包含问题信息的文件:

 public Question(String question, int possibleAnswers, String[] answers, int correctAnswer) {
    this.question = question;
    this.possibleAnswers = possibleAnswers;
    this.answers = answers;
    this.correctAnswer = correctAnswer;
}

我很困惑我应该如何生成每个对象,而循环通过这个是我已经得到的方法:

Which of the following is not a programming language?
4
Python
Java
PHP
SQL
4

3 个答案:

答案 0 :(得分:2)

我会将问题分解为:createQuestionsbuildQuestiongetPossibleAnswers等方法。这基本上就是你要做的事情,所以只用代码代表它。就像这样(留下有趣的部分,因为你是一名学生,这是一项任务:)):

public static String[] getPossibleAnswers(Scanner scanner, int possibleAnswers) {
    // Get the possible answers by using the passed in int and scanner...
}

public static Question buildQuestion(Scanner scanner) {
    // Step through the first couple lines building your new question
    // and get q.question and q.possibleAnswers...
    // Now get the possible answers using the helper method
    q.answers = getPossibleAnswers(scanner, q.possibleAnswers);
    // Finish up
    return q;
}

public static ArrayList<Question> createQuestions(String filename) throws IOException {

    ArrayList<Question> questions = new ArrayList<>();

    Scanner fileReader = new Scanner(new File(filename));
    // Every time the reader has a next, it's another question
    while (fileReader.hasNext()) {
        // Here's a question
        questions.add(buildQuestion(fileReader));
    }
    fileReader.close();
    return questions;
}

答案 1 :(得分:1)

如果你真的想把文件视为索引,那么这就是一种方法。当您读取文件时,请以7行的块为单位将其添加到字符串数组中。有点像你一样。

while (fileReader.hasNext()) {
    String[] lines = new String[7];
    for (int i = 0; i < 7 && fileReader.hasNext(); i++) {
        // Fill the array with the lines that make up the current question
    }
    questions.add(new Question(lines);
}

现在,提供构造函数以匹配或拥有一个带有字符串数组并返回问题的构建器。 (我会选择构建器)无论哪种方式,你都会从数组移动到实际值,知道有些实际上是整数。

答案 2 :(得分:-1)

您参考的功能。但建议您将问题列表保存在Excel文件中,以便格式化,易于阅读也可以维护。

public List<Question> createQuestion(String fileName){
        BufferedReader br = new BufferedReader(new FileReader(new File("")));
        String line = null;
        int lineIndex  = 0;
        Question q = null;
        List<Question> listQ = new ArrayList<>();
        String[] answers = new String[4];
        while ((line = br.readLine()) != null){     
            if (lineIndex >= 7){
                lineIndex = 0;
                listQ.add(q);
            }
            switch (lineIndex){
            case 0:
                q = new Question();
                q.setQuestion = br.readLine();
                lineIndex ++;
                break;
            case 1:             
                q.setpossibleAnswers = br.readLine();
                lineIndex ++;
                break;
            case 2:
                answers[0] = br.readLine();
                lineIndex ++;
                break;
            case 3:
                answers[1] = br.readLine();
                lineIndex ++;
                break;
            case 4:
                answers[2] = br.readLine();
                lineIndex ++;
                break;
            case 5:
                answers[3] = br.readLine();
                q.setAnswer(answers);
                lineIndex ++;               
                break;
            case 6:
                q.correctAnswer = br.readLine();
                lineIndex ++;
                break;
            default:
                break;
            }       
        }
    }