-3

This is a quiz app in Java. I am trying to compare the answer text stored as a variable (answeredOne) to the actual answer Prophase. I cannot get them to be equal. Do you see anything wrong with this code?

    EditText questionOne = (EditText) findViewById(R.id.answer_one);
    String answeredOne = questionOne.getText().toString();
    if (answeredOne == "Prophase") {
        score = score + 1;
    }

I keep getting zero when answering in the question.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
user3590371
  • 11
  • 1
  • 1

2 Answers2

5

You can't use == for strings. That would be checking the reference but not the value. Use .equals();

if (string.equals("otherstring")) {
    // Do something here
}
Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45
basic
  • 3,348
  • 3
  • 21
  • 36
0

Android uses java, so u can use .equals method.

   EditText questionOne = (EditText) findViewById(R.id.answer_one);
   if (questionOne.getText().toString().equals("Prophase")) {
      score = score + 1;
   }
Shank
  • 1,387
  • 11
  • 32