-3

So I need to compare my string with string that user inputs in EditText. I input same string but Java thinks that this is two different strings. I checked on spaces and didn't find them.

private void checkEditTexts(){
    ...
    String userAnswer = editTextAnswer.getText().toString().toLowerCase();
    String rightAnswer = getResources().getString(R.string.question3_answer1).toLowerCase();

    Log.v("checkET.userAnswer", "userAnswer = [" + userAnswer + "] rightAnswer = [" + rightAnswer + "]"  );

    if(userAnswer == rightAnswer) {
        points++;
        Log.v("checkEditTexts()", "POINTS++: " + points);
    }else{
        points--;
        Log.v("checkEditTexts()", "POINTS--: " + points);

    }
    ...
}

Logcat:

08-29 16:47:27.494 16583-16583/....V/checkET.userAnswer: userAnswer = [song 2] rightAnswer = [song 2]

08-29 16:47:27.494 16583-16583/... V/checkEditTexts(): POINTS--: 2

08-29 16:47:27.494 16583-16583/....V/checkEditTexts(): POINTS--: 1

08-29 16:47:27.494 16583-16583/....V/checkEditTexts(): POINTS: 1

You can see that userAnswer and rightAnswer are equal and it should perform points++, but instead it does points--

Sorry for bad English.

Personal Jesus
  • 133
  • 2
  • 10
  • @Rotwang It wasn't me. I thankful all of you for helping. I chose as correct answer first one with explanation and upvote other. – Personal Jesus Aug 29 '16 at 14:34
  • I never suspected **you**. You don't even have enough reputation to downvote. – Phantômaxx Aug 29 '16 at 14:35
  • @Rotwang "but one": wrong, I downvoted every reputation farming answer. Users with enough reputation and an older account (more than one year old) should already know that they aren't supposed to duplicate answers other and other again. You should know that too. – Tom Aug 29 '16 at 14:36
  • @Tom Aren't you way too strict? Just don't upvote the reputation farmers. At maximum, leave them a comment. Downvoting seems a bit too much. And... you forgot **one** answer. Which instead has been UPvoted. – Phantômaxx Aug 29 '16 at 14:40
  • @Rotwang Maybe yes, maybe no. Imo it ins't helpful to answer the same question over and over again and "not helpful" is a valid DV reason. There are also several meta discussions about that. And I also learned that leaving a comment has a very low chance of being useful. It mostly ends up in arguing, revenge downvotes and "evil community" accuses. There are currently 4 answers (one deleted) and every one has a DV. I "spare" answers of new users, who don't know how this site works. They also get the comment, since the success rate is higher there. – Tom Aug 29 '16 at 14:45
  • @Tom Well, if the question is closed (as a duplicate **should be**), then it becomes less visible. If it's also deleted, then it becomes completely invisible to users with less than 10K rep. And about "sparing new users"... that user has a 1300+ rep. I wouldn't consider that one a "new" user. – Phantômaxx Aug 29 '16 at 14:48
  • @Rotwang *"And about "sparing new users"... that user has a 1300+ rep. I wouldn't consider that one a "new" user. "* As I said, I can't see another answer which hasn't a DV. And the only user with that rep is abbath and his score is 2/1. Maybe the answer you mean has been deleted already? (it should be 6 answers then, since another answer (also with a DV) has been deleted already) – Tom Aug 29 '16 at 14:52
  • @Tom OK, I just didn't get into the vote history, my bad. – Phantômaxx Aug 29 '16 at 14:55
  • @Rotwang No worries. I'm sure it was a caching issue :D. – Tom Aug 29 '16 at 14:56
  • @Tom Ehr... Yes, of course. :D – Phantômaxx Aug 29 '16 at 14:57

4 Answers4

2

use equels or equalsIgnoreCase. == compare the the reference type not value. but equels and equalsIgnoreCase checks for value not reference.

if(userAnswer.equalsIgnoreCase(rightAnswer)) {
points++;
    Log.v("checkEditTexts()", "POINTS++: " + points);
}else{
    points--;
    Log.v("checkEditTexts()", "POINTS--: " + points);

}
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
1

you shouldn't use == when comparing strings.

if(userAnswer.equals(rightAnswer)) { //since you did toLowerCase you can use equals instead of equalsIgnoreCase

Henrik
  • 1,797
  • 4
  • 22
  • 46
1

In your if statements use the equals method for String comparison, because in Java, when the == operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory.

Modify your if like this:

...
if(userAnswer.equals(rightAnswer)) {
   points++;
   Log.v("checkEditTexts()", "POINTS++: " + points);
}
...
abbath
  • 2,444
  • 4
  • 28
  • 40
0

Try this

if (userAnswer.equalsIgnoreCase (rightAnswer)) {
    points++;
    Log.v("checkEditTexts()", "POINTS++: " + points);
}else{
    points--;
    Log.v("checkEditTexts()", "POINTS--: " + points);

}
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49