2017-07-30 103 views
1

我正在研究一個將成爲瑣事遊戲的項目。我需要創建一個循環遍歷文本文件並創建一個對象然後將其添加到ArrayList的方法。該文件的每7行都是一個新對象。對象本身是一個包含的東西,如可能的答案等問題如何循環Java中的文本文件,將7行對象添加到對象數組中?

我的對象構造:

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

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

我通過有像這樣的問題信息文件需要循環

我很困惑我應該如何產生每個對象,而循環通過這是就我已經得到的方法:

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

    ArrayList<Question> questionObj = new ArrayList<Question>(); 
    Question questionArray[] = null; 

    Scanner fileReader = new Scanner(new File(filename)); 
    while (fileReader.hasNext()) { 
     for (int i = 0; i <= 10; i++) { 
      questionArray[i] = new Question(); 

     } 
    } 
    fileReader.close(); 
+0

爲什麼不把對象存儲爲XML或JSON?很多庫支持序列化/反序列化。 – mre

+0

它正在處理的任務,所以它必須來自.txt文件 –

+0

明白了,謝謝。 – mre

回答

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; 
} 
+0

你能否爲我分解一下buldQuestion方法,這就是我最苦惱的部分。 –

+0

你基本上看文件結構並處理每一行。所以,第一個是問題,第二個是可能答案的數量。繼續,然後讓他們,但你需要。然後使用可能的答案數從文件中獲取它們並將它們放入數組中。剩下的更多來自@ChaceMcguyer。 – ChiefTwoPencils

+0

那麼我需要在該方法中使用掃描儀嗎?我希望事情和Python一樣簡單。爲了確保我理解我通過索引0 - >索引6(前7行)中的文檔解析並在buildQuestion方法中創建Question對象。然後我只是調用該方法並將其添加到問題數組列表中? –

-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; 
      }  
     } 
    } 
+0

最終會不會每個問題都有相同的答案? – ChiefTwoPencils

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); 
} 

現在,提供構造函數來匹配或使用構造函數來獲取字符串數組並返回問題。 (我會選擇建設者)無論哪種方式,你都會從數組移動到實際值,知道有些實際上是整數。

相關問題