6

My program displays an array of questions (question one-by-one). After I write an answer, an alert message should tell me whether my answer is right or wrong. The problem is that, even if I write the right answer the alert message displays a "false" message.

final String questions[] = {"Who's Tom?", "Who's Luca?", "Who's Flavie?"}
final String answers[] = {"American", "Italian", "French"}

// display question
answer_question.setOnClickListener(new View.OnClickListener() {
    int CurrentQuestionIndex = 0;

    public void onClick(View v) {
        ask_question.setText(question[(CurrentQuestionIndex++) % (questions.length)]);

        // discuss question versus answer
        EditText answer = (EditText) findViewById(R.id.tvReponseF);

        if (answer.equals(answers[CurrentQuestionIndex])) {
            alertMessageRight();
        } else {
            alertMessageFalse();
        }
    } 
});
1
  • Posts your answers array. Commented Jul 8, 2016 at 19:03

5 Answers 5

6

The problem is that you're comparing an EditText object with a String field. You must compare String with String instead.

Here's how to do it:

String answer = ((EditText) findViewById(R.id.tvReponseF)).getText().toString();

if(answer.equals(answers[CurrentQuestionIndex]))
{
     ...
Sign up to request clarification or add additional context in comments.

1 Comment

that's not all, that's just a minor issue. He's increasing the index counter at the wrong place, check my answer, I covered that also.
3

The problem is that you're increasing the index before actually checking if the answer's correct:

ask_question.setText(question[(CurrentQuestionIndex++) % (questions.length)]);

So the if the question index is 0, you'll get a question 0, then the question index will become 1 because of the ++ operator and you'll be reading the answer 1 instead of 0. I hope you understand this. What you need to do is remove the ++ from here and place it here:

if(answer.getText().toString().equals(answers[(CurrentQuestionIndex++) % (questions.length)]))

Because at this point, you'll read the proper answer and move on to the next index.

EDIT:

This was your biggest problem. Also you need to compare the answer.getText().toString() as the other guys already noticed.

Comments

3

You need to get the text from the EditText object. You are comparing an EditText object with a String.

So, your complete code should look like this:

final String questions[] = {"Who's Tom?", "Who's Luca?", "Who's Flavie?"}
final String answers[] = {"American", "Italian", "French"}

// display question
answer_question.setOnClickListener(new View.OnClickListener() {
int CurrentQuestionIndex = 0;
    public void onClick(View v) {

        ask_question.setText(question[(CurrentQuestionIndex++) % (questions.length)]);
        // discuss question versus answer
        EditText editText = (EditText) findViewById(R.id.tvReponseF);
        if(editText.getText().toString().equals(answers[CurrentQuestionIndex]))  
        {
            alertMessageRight();
        } else {
            alertMessageFalse();
        }
    } 
 });

Comments

2

Try this:

EditText et;//initialize first!!
et.getText().toString().equals(....);

Comments

2
 EditText answer = (EditText) findViewById(R.id.tvReponseF);

 if(answer.getText().toString().toLowerCase().equals(answers[CurrentQuestionIndex].toLowerCase()))  
{
    alertMessageRight();
} else {
    alertMessageFalse();
}

 } 

I added the toLowerCase() just make the problem case insensitive.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.