0

I can successfully display the first problem from the text file, however I am having trouble accepting that answer and then displaying the second problem. I used a for loop in order to read lines from the text file. I also have an onClick listener.

My major problem is, how do I have the for loop wait until the onClick listener is hit before looping into the second problem.

Here's the relevant code:

public class MathTestActivity extends AppCompatActivity {

String correctAnswer;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mathtest);

    TextView mathProblem = (TextView) findViewById(R.id.mathProblem);
    final EditText mathAnswer = (EditText) findViewById(R.id.mathAnswer);

    //Stying for the question text
    mathProblem.setTextSize(40);
    mathProblem.setTextColor(Color.rgb(0, 0, 0));

    //Try to read the problem and answers text file
    try {
        InputStream is =      this.getResources().openRawResource(R.raw.mediummath);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;

        int n = 20; /*How many rows this test has*/
        for (i = 0; i < n; i++) {
            if ((line = reader.readLine()) != null)
                mathProblem.setText(line);
            if ((line = reader.readLine()) != null)
                correctAnswer = line;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Button enterButton = (Button) findViewById(R.id.enterButton);
    enterButton.setOnClickListener(new View.OnClickListener() {

        @Override
            public void onClick(View view) {
                int correctcount = 0;
                String answer = mathAnswer.toString() ;//need to turn answer into string Integer.parseInt(mathAnswer.getText().toString());
                if (answer == correctAnswer){
                    Toast.makeText(MathTestActivity.this,
                                    R.string.correct_toast,
                                    Toast.LENGTH_SHORT).show();

                    correctcount++;
                    i++;
                }
                else{
                    Toast.makeText(MathTestActivity.this,
                            R.string.incorrect_toast,
                            Toast.LENGTH_SHORT).show();
                    i++;
                }
        }
    });

    //Add layout for this activity
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.mtestcontent);
    layout.addView(mathProblem);
    layout.addView(mathAnswer);

}
}

As you can see, it's the logic inside the "try" portion that I'm having difficulty with. Can someone help me with the logic here?

fadden
  • 51,356
  • 5
  • 116
  • 166
Milap
  • 147
  • 1
  • 6
  • 15
  • 2
    You wouldn't, you're structuring your entire approach wrong. You need to write it in an event driven manner- your code reacts to events (like button clicks) rather than waiting for them. This is how GUI ode generally operates. – Gabe Sechan Apr 29 '16 at 22:45
  • 1
    Sidenote: `answer == correctAnswer` ==> [How to compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OneCricketeer Apr 29 '16 at 22:46

1 Answers1

2

You need to "react" to an event. Your current code will read the entire file, then only display the last question & answer.

A recommended approach would be the following logic.

  • Read your entire file into a list of (question, answer) pairs that you can index later.
  • Load the first question
  • Declare a button listener
    • Compare the answer given to the correct answer
      • If correct, load the next question
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you! This makes sense. – Milap Apr 29 '16 at 23:08
  • Hey Cricket, I'm a little confused. If I set up the button listener, my app will listen for the button click, compare answers, tell you if you're right, and then load next question. However, after the first click, the 3rd question never loads. Do you understand why? Should I have a for loop around my button listener? – Milap Apr 30 '16 at 02:51
  • The only for-loop you need should be to read the file. I assume you have some `private int questionIndex = 0;` and some `private List questions;` in your Activity? If so, then you should only have to do `Question nextQuestion = list.get(questionIndex++);` and load `nextQuestion` into your views. – OneCricketeer Apr 30 '16 at 03:37