-1

Good day everyone, so i am writing a quiz app and score is based on what the user selects. I used radio buttons, check box and edit text tags for answering questions. When the user clicks a radio button, or a check box, the score updates, but when the user enters the correct text in the EditText field, the score does not update. I used an if else statement to check if the string entered matches an answer.

    public void startFinalScreen(View v) {

    EditText text = (EditText) findViewById(R.id.edit_text_view_answer);
    int questionNineScore = getIntent().getIntExtra("GNS", 0);
    int finalScore = questionNineScore;
    String answer = "William Shakespeare";
    if (text.getText().toString() == answer) {
        qNineScore = qNineScore + 1;
    }
    finalScore = qNineScore;
    }

I used .getIntent() to get score from a previous activity. After i do this, the score does not add +1 even after typing the correct answer.

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
William
  • 13
  • 3
  • `Strings` are immutable, so `==answer` should be `.equals(answer)`. See these two for more information: [What is the difference between == vs equals() in Java](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) and [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Kevin Cruijssen Jun 28 '18 at 13:23
  • I dont get how this question is marked as duplicate. Its obvious that the questioneer did not know that the error was with how to compare strings. – mrfr Jun 28 '18 at 13:28

1 Answers1

1

Instead of

text.getText().toString() == answer

use

if (text.getText().toString().equals(answer)) {
    qNineScore = qNineScore + 1;
}
Sagar
  • 23,903
  • 4
  • 62
  • 62