1091 Frågespel - QuestionReader

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

      /**
       * Used to read questions and answers from file.
       * 
       * @author Rikard Karlsson 
       * @version 2014-11-04
       */
      public class QuestionReader
      {
              private String fileName;

              public QuestionReader(String myFileName)
              {
                      fileName = myFileName;
              }
              /**
               * Read questions and answers from file. 
               * 
               * @return An ArrayList containing objects of the class Question.
               */
              public ArrayList<Question> readQuestions()
              {
                      //questions and aswers are read from this file
                      //the first line in the file is a question
                      //the second line is an answer
                      //the third line is a question
                      //the fourth line is qn anser
                      // and so on

                      //a list to store read questions and answers in
                      ArrayList<Question> questionBook = new ArrayList<Question>();
                      try
                      {
                              FileReader fr = new FileReader(fileName);
                              BufferedReader inFile = new BufferedReader(fr);
                              //read the first question and answer
                              String question = inFile.readLine();
                              String answer = inFile.readLine();
                              while(answer != null) //there is more questions and answers
                              {
                                      questionBook.add(new Question(question, answer));
                                      //read the next question and answer
                                      question = inFile.readLine();
                                      answer = inFile.readLine();
                              }
                              inFile.close();
                      }
                      catch(FileNotFoundException nfe)
                      {
                              System.out.println("File not found");
                      }
                      catch(IOException ioe)
                      {
                              System.out.println("error; "+ ioe);
                      }
                      return questionBook;
              }
      }