Search code examples
javabufferedreader

BufferedReader skip questions on a test/quiz


Alright, so I'm taking on this little personal project for my teacher (Not my Computer Science teacher) and I'm trying to make a test from a fairly large list of questions. So far I've been able to read in the first question, ask the answer, and tell if they're right or not. The only problem is, it repeats the same question over and over.

Sadly, I'm stumped as to how I can make the program check to see if the question has been answered or not. My initial thought was to take the line from the question file that says "QUESTION #", separate the number, parse it into an integer and somehow make the BufferedReader skip that question.

Here is the quizTest:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class quizTest {
    public static void main (String[] args) throws IOException{

        BufferedReader reader = null;
        reader = new BufferedReader(new FileReader("C:/Users/Kevin/Desktop/eclipse/WorkspaceCompTIA.ActualTests.220-802.v2012-11-05.by.Caitto.txt/"));

        String line, userAnswer;
        int i = 0;

        Scanner scan = new Scanner(System.in);

        String questionNum = "";
        String question = "", choiceA = "", choiceB = "", choiceC = "", choiceD = "", answer = "";

        while((line = reader.readLine()) != null){

            if(line.contains("?")){
                question = line;
            }

            if(line.contains("A.")){
                choiceA = line;
            }

            if(line.contains("B.")){
                choiceB = line;
            }

            if(line.contains("C.")){
                choiceC = line;
            }

            if(line.contains("D.")){
                choiceD = line;
            }

            if(line.contains("Correct Answer: ")){
                String[] a = line.split(": ");
                answer = a[1];
            }
        }




        while(i < 274){

            quizClass run = new quizClass(i, question, choiceA, choiceB, choiceC, choiceD, answer);

            System.out.print(questionNum);
            System.out.println(run.getQuestion());
            System.out.println();
            System.out.println(run.getChoiceA());
            System.out.println(run.getChoiceB());
            System.out.println(run.getChoiceC());
            System.out.println(run.getChoiceD());
            System.out.println();

            System.out.print("Your Answer: ");
            System.out.println();
            userAnswer = scan.next();

            if(userAnswer.equalsIgnoreCase(answer)){
                System.out.println("Correct!");
                i++;
            }
        }

        if(i == 274){
            reader.close();
            scan.close();
        }
    }
}

Here is quizClass:

public class quizClass {

    private String question, answer, choiceA, choiceB, choiceC, choiceD;
    private int questionNum;

    public quizClass(int questionNum, String question, String choiceA, String choiceB,              String choiceC, String choiceD, String answer){
        this.question = question;
        this.answer = answer;
        this.choiceA = choiceA;
        this.choiceB = choiceB;
        this.choiceC = choiceC;
        this.choiceD = choiceD;
    }

    public String getQuestion(){
        return question;
    }

    public String getAnswer(){
        return answer;
    }

    public String getChoiceA(){
        return choiceA;
    }

    public String getChoiceB(){
        return choiceB;
    }

    public String getChoiceC(){
        return choiceC;
    }

    public String getChoiceD(){
        return choiceD;
    }

    public int getQuestionNum(){
        return questionNum;
    }


}

And finally here is the format of the question file:

http://puu.sh/5nJhS.png enter image description here

EDIT:

So after fiddling around with it, I've managed to get it working now(I'm sure there are better ways to go about it, but this way works, so I'm happy :P):

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class QuizTest {
public static void main (String[] args) throws IOException{

    String question = "";
    String choiceA = "";
    String choiceB = "";
    String choiceC = "";
    String choiceD = "";
    String answer = "";
    String line;
    String userAnswer;
    int i = 0;
    BufferedReader reader = null;           

        reader = new BufferedReader(new FileReader("C:/Users/Kevin/Desktop/eclipse/Workspace/CompTIA.ActualTests.220-802.v2012-11-05.by.Caitto.txt/"));
        Scanner scan = new Scanner(System.in);

        QuizClass run = new QuizClass(i, question, choiceA, choiceB, choiceC, choiceD, answer);

        while((line = reader.readLine()) != null){

            //Finds the question
            if(line.contains("?")){
                run.question = line;
                System.out.println(run.getQuestion() + "\n");
            }

            //Finds answer "A"
            if(line.contains("A.")){
                run.choiceA = line;
                System.out.println(run.getChoiceA());
            }

            //Finds answer "B"
            if(line.contains("B.")){
                run.choiceB = line;
                System.out.println(run.getChoiceB());
            }

            //Finds answer "C"
            if(line.contains("C.")){
                run.choiceC = line; 
                System.out.println(run.getChoiceC());
            }
            //Finds answer "D"
            if(line.contains("D.")){
                run.choiceD = line;
                System.out.println(run.getChoiceD() + "\n");
            }

            //Finds the correct answer for the question
            if(line.contains("Correct Answer: ")){
                String[] a = line.split(": ");
                answer = a[1];
                run.answer = answer;

                System.out.print("Your Answer: ");
                userAnswer = scan.next();

                    //Checks if the user's input matches the correct answer from the file
                    if(userAnswer.equalsIgnoreCase(answer)){
                        System.out.println("Correct!\n");
                        i++;
                    } 

                    //Checks if the user's input doesn't match the correct answer from the file
                    else if (!userAnswer.equalsIgnoreCase(answer)) {
                        System.out.println("Wrong, the correct answer was: " + run.getAnswer());
                        i++;
                    }

            }


        }

                if(i == 274){
                    reader.close();
                    scan.close();
                }
}
}

Solution

  • You're reading in the whole file in your initial while loop and writing over your variables. So, to simplify, the program will read the line

    A. NETSTAT
    

    and set choiceA to "A. NETSTAT". It will then continue looping until it sees

    A. Windows XP
    

    and then update choiceA to be "A. Windows XP".

    In the end, it will be like your program only read the last question.

    How should you solve this? My first thought would be to encode your questions in XML, from which you load a set of questions. This is a less clunky solution but requires a bit more infrastructure.

    If you want to stay with your current set up, you should have two loops: one outer loop the runs until your reach EOF, and an inner that loops until the "Correct Answer" line.

    Extra tips/comments:

    • Add a toString() method QuizClass (note the standard Java naming scheme -- always captialize) like this:

      public toString() {
        System.out.print(questionNum);
        System.out.println(question);
        System.out.println();
        System.out.println(choiceA);
        System.out.println(choiceB);
        System.out.println(choiceC);
        System.out.println(choiceD);
        System.out.println();
      }
      

    and then call it in main with

    System.out.print(run);
    
    • answer variable in your QuizClass class does nothing. You instead use another variable, userAnswer, in the main class. Why?

    • What is 274? Don't use magic numbers that don't mean anything. There's probably a constant defined in the classes you are using that are more informative.