18

here's my code, Eclipse doesn't show any errors, program's working fine, but it simply doesn't do exactly what i want:)

    View image_view_danger_rate = (ImageView) findViewById(R.id.origin);
    View image_view_origin = (ImageView) findViewById(R.id.danger_rate);

    String entry_tag = (String) descriptionResultView.findViewById(resID).getTag();

    String dangerous = "dangerous";
    String not_dangerous = "not_dangerous";

    if ( entry_tag == dangerous) {
        image_view_danger_rate.setBackgroundResource(R.drawable.attention);
    }else if ( entry_tag == not_dangerous) {
        image_view_danger_rate.setBackgroundResource(R.drawable.its_ok);
        image_view_origin.setBackgroundResource(R.drawable.artificial);
    }

The application should choose between two images to pop-up on the screen, depending on a tag stored in the xml file. So, if the tag says "dangerous", then should be shown the "attention"-image. If the tag says "not_dangerous", there should be the "its_ok"-image.

Now, displaying the images without an if-comparison works perfectly.

If i print out the tags as a string, it works, it prints correctly "dangerous" or "not_dangerous", depending on the tag.

But if there's a if-comparison as shown above, nothing happens, no image is shown.

Please anyone help!!=)

Erich Douglass
  • 51,744
  • 11
  • 75
  • 60
Silvan
  • 189
  • 1
  • 1
  • 3
  • +1 for an elaborate and complete question. – Marcelo Jul 25 '11 at 17:01
  • This question has been asked on this site more times than I care to find. Please use the search function. – Woot4Moo Jul 25 '11 at 17:29
  • @Woot4Moo Duplication is not necesarilly bad. http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – Marcelo Jul 25 '11 at 17:34
  • @Woot4Moo Your link to the supposedly duplicate question does not even have to do with this question too. – Marcelo Jul 25 '11 at 17:37
  • @Marcelo even still how is this subtly different than any of the other examples on this site about people not knowing how to do string comparison in java? – Woot4Moo Jul 25 '11 at 17:37
  • well too late now to change my vote to close because of no new edit: http://stackoverflow.com/questions/995918/java-string-comparison – Woot4Moo Jul 25 '11 at 17:38
  • let's not confuse `comparison` with `equality` – msysmilu Feb 03 '15 at 19:24

4 Answers4

45

Use string1.equalsIgnoreCase("something) or .equals("Something");

With == (for strings) in java you are comparing they are of same reference. Like you did is the test if both of them are strings objects.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • I just learned something very important! =D it worked for me...thank you for this quick answer! – Silvan Jul 25 '11 at 17:00
13

In java, a==b is used to compare 2 references, not the objects themselves.

so if you have 2 strings that you want to compare, use the equals() method on String. for eg

boolean resultOfComparison=stringA.equals(stringB);
Archit
  • 887
  • 2
  • 10
  • 19
5

Use

entry_tag.equals(dangerous)

you're comparing actual String objects, not their content. In Java, operators are not overloaded so == can't be used to compare strings.

diciu
  • 29,133
  • 4
  • 51
  • 68
2

In Java, if you want to compare Strings, you need to use equals():

if (entry_tag.equals(dangerous)) {
}
Erich Douglass
  • 51,744
  • 11
  • 75
  • 60