1

I'm not experienced in Java, and I have a problem.

Using Jsoup, I have an element called td. If I do:

String attr = td.attr("class");
System.out.println(attr);

The output is "free", which is perfectly alright. If I do:

String attr = td.attr("class");
if (attr == "free") {
System.out.println("freedom!");
}

There is no output!

Does anyone know how to solve this problem?

Thanks in advance.

ian
  • 193
  • 2
  • 10
  • @Rohit Jain Yes, you are right. Because I am new to Java, I didn't see this, because I thought it was a problem with Jsoup specifically. Now I see the answer I understand that my thoughts were completely wrong. – ian Jul 01 '13 at 13:02

1 Answers1

7

You have to compare the string using the equals method as == compare references, not strings contents.

String attr = td.attr("class");
if (attr.equals("free")) {
    System.out.println("freedom!");
}
Jonathan Naguin
  • 14,526
  • 6
  • 46
  • 75
  • Thank you, that worked! When I see it like this, it's logical, but I'm not experienced in Java. – ian Jul 01 '13 at 12:54