Search code examples
javasubstringbufferedreaderstringbuilderfilereader

How to get test questions from txt file with specific structure?


I want to make a program which get questions and their answers from file with specific structure and let the user give answers to them. The program also have to count right answers and show them to the user.

Here is the sample of the text file structure:

What year is it right now?
2
1) 1900
2) 2014
3) 3200
---
Which is the biggest country in the world?
1
1) Russia
2) United States of America
3) United Kingdom
---

That's the code I wrote, but there something wrong and I can't see what exactly is:

public class testLoader {

private static BufferedReader br;
private static int answerCounter;

public static void main(String[] args) {

    try {
        br = new BufferedReader(new FileReader("D:/test.txt"));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            String answer=null;
            if(line.startsWith("*")){
                answer = line;
            }
            while (line != "---") {
                line = br.readLine();
                sb.append(line);
                sb.append(System.lineSeparator());
            }
            System.out.println(sb.toString());
            answerCheck(answer);

            line = br.readLine();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    System.out.println("You have " + answerCounter + "correct answers");
}

public static void answerCheck(String rightAnswer) {
    System.out.println("What's your answer?");
    Scanner input = new Scanner(System.in);
    String answer = input.nextLine();
    answerCounter = 0;
    if (answer == rightAnswer){
        answerCounter++;
        System.out.println("Correct!");
    } else {
        System.out.println("Wrong!");
    }
}

}

I'll apreciate any help you can give. If there are any better way to complete the task, I'll be glad to see it.

Thanks in advance!


Solution

  • Here is the corrected version of your program. You had a few bugs in there. My program works OK on the file format you posted here (without the asterisks that is).

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class testLoader {
    
        private static BufferedReader br;
        private static int answerCounter = 0;
    
        public static void main(String[] args) {
    
            try {
                br = new BufferedReader(new FileReader("C:/Various/test.txt"));
                StringBuilder sb = new StringBuilder();
                String line = null;
    
                do {
                    line = br.readLine();
                    if (line != null) {
                        sb.append(line);
                        sb.append(System.getProperty("line.separator"));
                    } else {
                        break;
                    }
                    String answer = br.readLine().trim();
                    while (!"---".equals(line)) {
                        line = br.readLine();
                        sb.append(line);
                        sb.append(System.getProperty("line.separator"));
                    }
                    System.out.println(sb.toString());
                    answerCheck(answer);
                    sb.setLength(0);
    
                } while (true);
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            System.out.println("You have " + answerCounter + " correct answers");
        }
    
        public static void answerCheck(String rightAnswer) {
            System.out.println("What's your answer?");
            Scanner input = new Scanner(System.in);
            String answer = input.nextLine();
            // answerCounter = 0;
            if (answer.equals(rightAnswer)) {
                answerCounter++;
                System.out.println("Correct!");
            } else {
                System.out.println("Wrong!");
            }
        }
    
    }