-1
private void refineWords() {
    for(String word : words){
        Log.i("word", word);
        if (word == "s" || word == "t" || word == "am" || word == "is" || word == "are" || word == "was" || word == "were" || word == "has" || 
            word == "have" || word == "been" || word == "will" || word == "be" || word == "would" || word == "should" || word == "shall" || 
            word == "must" || word == "can" || word == "could" || word == "the" || word == "as" || word == "it" || word == "they" ||
            word == "their" || word == "he" || word == "she" || word == "his" || word == "her" || word == "him" || word == "its" ||
            word == "in" || word == "on" || word == "a" || word == "at") {

            Log.i("step", "step Success!!");
            words.remove(word);
        }
    }
}

I have a List called "words" and it contains strings. Here the Log.i works for the "word" tag fine but the "step" Statement does not executes. Seems the If condition does not work well. like this method never goes into it although the "words" list contains similar strings. What would be the problem. pleas help..

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
ssrp
  • 1,126
  • 5
  • 17
  • 35
  • See also http://stackoverflow.com/questions/767372/java-string-equals-versus and http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java and http://stackoverflow.com/questions/995918/java-string-comparison and http://stackoverflow.com/questions/658953/if-statement-with-string-comparison-fails ... – Christoffer Hammarström Mar 27 '12 at 12:31

4 Answers4

19

You need to use String.equals(), not ==. == checks if two Object references refer to the same Object:

if("s".equals(word) || "t".equals(word) || ...

From section 15.21.3 Reference Equality Operators == and != of the Java Language Specification 3.0:

While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Could not figure..Got a lot in my mind !!Thank you. – ssrp Mar 26 '12 at 11:13
  • This indeed works as advertised though I prefer to switch the two. `"s".equals(word)` has no risk of throwing a `NullPointerException`. – Dennis Laumen Mar 26 '12 at 11:16
  • @DennisLaumen, agreed. Updated answer. – hmjd Mar 26 '12 at 11:17
  • 2
    - 1 for the 'yoda condition' on the revised version of the answer – Nikola Yovchev Mar 27 '12 at 10:23
  • 2
    @baba, the 'yodo condition' (never heard that before) protects against a `NullPointerException` if `word` is `null`. I prefer this style and mistakenly did not use it in my original answer. Why -1? The answer is correct. – hmjd Mar 27 '12 at 10:27
  • @baba, can you supply a reason for -1 due to yoda condition? According to http://stackoverflow.com/questions/3418680/java-equals-ordering, it is acceptable. – hmjd Mar 27 '12 at 11:02
  • @hmjd I personally find the null == x condition harder to read. Consider the following words: 'If bird is eagle' vs 'If eagle is a bird'. Here is a discussion of a similar matter: http://stackoverflow.com/questions/7434058/null-variable-checking-in-string . However, the real reason I downvoted the answer is because I am somewhat of a code purist and while the answer is correct, its first and unedited version appealed more to me. – Nikola Yovchev Mar 27 '12 at 11:18
  • @hmjd there is nothing wrong for me neither with the Yoda condition. It is useful, and any Java developer can spot what's going on. I gave you back a +1. – Alex Mar 27 '12 at 11:19
  • @baba, this is not using `==`. – hmjd Mar 27 '12 at 11:20
  • @hmjd Semantically speaking, he 'is' using == :) as to a person not that familiar with Java '==' is equivalent to the equals method. – Nikola Yovchev Mar 27 '12 at 11:22
  • +1 from me too. In cases where `null` should result in `false` and doesn't need to be handled explicitly, why not use the "yoda condition" to make code a bit shorter and thus easier to read (at least for any somewhat experienced programmer). Also, being a code purist is not really a reason to downvote an otherwise correct answer. – Thomas Mar 27 '12 at 11:25
  • @hmjd As I already mentioned, I do not argue that the answer is incorrect. I just passionately abhor the reverse clause which wasn't in your original answer but appeared in the edit. Thus the downvote. – Nikola Yovchev Mar 27 '12 at 11:34
8

As the others said, you use object.equals(otherObject) to compare objects in Java.

But your approach is completely wrong.

Try instead

Set stopWords = new HashSet(Arrays.asList("s", "t", "am",
                                          "is", "are", "was", "were",
                                          "has", "have", "been",
                                          "will", "be", ...));

and then

private void refineWords() {
    words.removeAll(stopWords);
}

and you should be done.


Further, note that with your current code you will get a ConcurrentModificationException because you try to change the collection while you're iterating over it.

So if you can't use the abovewords.removeAll(stopWords), then you must instead use the much more verbose Iterator.remove() method:

private void refineWords() {
    for (Iterator<String> wordsIterator = words.iterator(); wordsIterator.hasNext(); ) {
        String word = wordsIterator.next();
        if (stopWords.contains(word)) {
            wordsIterator.remove();
        }
    }
}
Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58
3

In java you need to compare strings with equals:

if(word.equals("s") ...
Tudor
  • 61,523
  • 12
  • 102
  • 142
1

you might want to use equalsIgnoreCase(..) method for a finer refinement.

Tito
  • 8,894
  • 12
  • 52
  • 86