0

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!

Altair
  • 168
  • 1
  • 9
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Sotirios Delimanolis Jan 11 '14 at 22:02
  • What happens when you run the program, and how is it different from what you expect? – Etaoin Jan 11 '14 at 22:04
  • Thx I saw this about the equals(), but the code still don't work like I wanted to.. With equals insted of "!=" it only shows "What is your answer:" and check the answer. But it is always "WRONG" – Altair Jan 11 '14 at 22:06

2 Answers2

1

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

}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Something works wrong about StringBuilder object. You should check that :) – Mehmet Sedat Güngör Jan 11 '14 at 22:48
  • After an answer is given to the first question, doesn't it prompt first question with the second one? – Mehmet Sedat Güngör Jan 11 '14 at 23:00
  • Can you tell me what is the difference between "sb.append(System.getProperty("line.separator"));" and "sb.append(System.lineSeparator());" and why the first one is better? – Altair Jan 12 '14 at 00:11
  • @Crypto I don't think there's any difference. But I am on JDK 6, and I think this method was introduced with JDK 7. So I just don't have this method. If it works for you - then it's OK. So if you check here you'll notice there's no such method at all http://docs.oracle.com/javase/6/docs/api/java/lang/System.html – peter.petrov Jan 12 '14 at 00:13
1
  1. You want to seperate right answer from wrong ones with * sign but you are not providing it on your text file.
  2. Even if you add * sign to the head of right answer, you won't make right answer value assigned to answer string variable.
  3. Besides all of above, why do you want to check right answer with a * sign if you write the right answer's number below the question?
  4. You have to compare strings with equals() method since you are dealing with values of strings, not memory addresses of them.
  5. You have declared a StringBuilder object to append questions/answers and print them to screen but with this code, you will always add previous questions/answers to the current question. (i.e you are printing 2. question with first one above of it)
  6. answerCounter variable will not hold user's total correct answers as you always assign it to 0 whenever you call the method.

So with all of these are corrected, I think you want to achieve something like below:

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:/test.txt"));
        StringBuilder sb;
        String line, answer = null;

        while((line = br.readLine()) != null)
            {
            sb = new StringBuilder();
            do
                {
                if(line.length() == 1) //This is a bad choice of digit comparison but it will work with your case (lol)
                    {
                    answer = line;
                    line = br.readLine();
                    continue;
                    }
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
                }
            while(!line.equals("---"));

            System.out.println(sb.toString());
            answerCheck(answer);
            }
        }
    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();
    if(answer.equals(rightAnswer))
        {
        answerCounter++;
        System.out.println("Correct!");
        }
    else
        {
        System.out.println("Wrong!");
        }
    }
}