0

Possible Duplicate:
Java String.equals versus ==
comparison of two Strings doesn't work in android

I have a string array with values which I am trying to compare against text inputted into an edittext field. However despite both the edittext and the value in the array being the same my if (answer == guess) never resolves to true.

I've put debugging via Toast messages and it shows both answer and guess are the same. Can someone help with this please? Code below:

    final EditText et;

    et = (EditText) findViewById(R.id.editText1);
    String guess = et.getText().toString();
    String answer = LinesFromFile[LineNumber]; 

    if (answer == guess)
    {
        Toast msg = Toast.makeText(getBaseContext(), answer + " " + guess + " right", Toast.LENGTH_LONG);
        msg.show();
    }

    else
    {
        Toast msg = Toast.makeText(getBaseContext(), answer + " " + guess + " wrong", Toast.LENGTH_LONG);
        msg.show();         
    }
Community
  • 1
  • 1
automationguy
  • 313
  • 1
  • 5
  • 17

4 Answers4

3

You can't do string comparisons using == ... you need to use String.equals()

if (answer.equals(guess))
{
  ...
Greg Kopff
  • 15,100
  • 11
  • 53
  • 72
  • 1
    Ah, this is what happens when old C developers try to code new android java code :-( – automationguy Jul 06 '12 at 03:41
  • This is strange. Neither .equals or .contentEquals is working. Both return false yet, as I said before, the toast debug shows that both answer and guess have the same value. – automationguy Jul 06 '12 at 03:52
  • 1
    @automationguy: Does either have any leading/trailing whitespace (including newlines) - try: `if (answer.trim().equals(guess.trim()))` – Greg Kopff Jul 06 '12 at 03:57
  • I will give this a go. I've just started learning this stuff so I want to get to the bottom of the problem. You might be right about the newline. I'll try it and post back. – automationguy Jul 06 '12 at 04:01
  • Indeed Sir! That was the problem. Trimming and using .equals works a charm. :-) – automationguy Jul 06 '12 at 04:03
  • I have unpicked and picked yours. I hope the other guy doesn't get upset. :-) – automationguy Jul 07 '12 at 01:05
3

I suggest you don't use == and better use equals().

Parth Doshi
  • 4,205
  • 15
  • 73
  • 125
3

The == operator for Strings checks if the reference are equal or not, that's why its not working for you.

Use contentEquals()

The String.contentEquals() methods only compares the contents (the character sequence) and does not check if the other object is also an instance of String.

Or if you are sure the two are Strings then use equals()

The String.equals() not only compares the String's contents, but also checks if the other object is also an instance of a String.

Ashwin Singh
  • 6,987
  • 4
  • 34
  • 55
1

You cannot use '==' to compare strings

You can also use compare:

            if(username.compareTo(name) == 0) // if string matches

            {
            // add the code

            }

From javadoc:

public int compareTo(String anotherString)

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

 this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

 this.length()-anotherString.length()

Specified by:

compareTo in interface Comparable<String>

Parameters:

anotherString - the String to be compared.

Returns:

the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

Onots
  • 2,120
  • 21
  • 28
Avi Kumar
  • 4,961
  • 7
  • 37
  • 69
  • Aha! This worked! Now this is interesting. I wonder why the .equals and .contentEquals didn't work? – automationguy Jul 06 '12 at 03:58
  • i do not know exactly but i think equals should have worked but see this link would help to understand better http://leepoint.net/notes-java/data/expressions/22compareobjects.html – Avi Kumar Jul 06 '12 at 04:30