0

Im trying to read the value of an attribute called "version" from a tree. The value of that is "2.0". I set an if statement asking if the attribute value is equal to 2.0 to run this code, instead it runs the else statement. I got confused to i made a boolean value and set it equal to the equation. I print it out and it reads false when it is indeed true. Here is my code:

out.print("Enter the URL of an RSS 2.0 news feed: ");
        String url = in.nextLine();
        XMLTree xml = new XMLTree1(url);
        boolean t = xml.hasAttribute("version");
        if (t) {
         out.println(xml.attributeValue("version"));//this prints 2.0
         boolean a = (xml.attributeValue("version") == "2.0"); //added this to debugg
         out.println(a);  //this gets set to false. why?
            if (xml.attributeValue("version") == "2.0") {


                out.println("Hello");

            } else {
                out.println("URL entered is not of version 2.0");
            }
        } else {
            out.println("No attribute Version");
        }

        /*
         * TODO: fill in body
         */

        in.close();
        out.close();
    }

}

Im entering the URL: http://news.yahoo.com/rss/, which its tree has a root tag "rss" that has an attribute "version" who is equal to 2.0:

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
WestonBuckeye
  • 45
  • 2
  • 3
  • 11

1 Answers1

1

Change this:

boolean a = (xml.attributeValue("version") == "2.0");
. . .
if (xml.attributeValue("version") == "2.0") {

to:

boolean a = (xml.attributeValue("version").equals("2.0"));
. . .
if (xml.attributeValue("version").equals("2.0")) {

The == operator in Java tests for object identity, not value equality (which is what you need here).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I will accept this answer, but may i ask why java does not like "==" in an if statement ? Java beginner here. :/ – WestonBuckeye Sep 25 '13 at 02:41
  • @WestonBuckeye: why wouldn't Java like it? The `==` operator and the `.equals()` method just do different things. – Jeroen Vannevel Sep 25 '13 at 02:42
  • @WestonBuckeye - Java likes `==` just fine. It just means "are the left and right side the same object?" The `equals()` method means "does this object have the same value as the argument (by whatever logic this object wants to use to test for equality of value)?" In the case of `String` objects, the logic is that two strings have the same value if they contain the same sequence of characters. Your problem arises because two `String` objects in Java can have the same value without being the same object. – Ted Hopp Sep 25 '13 at 02:43
  • 1
    @WestonBuckeye - If you're familiar with C++, then this might help. For two objects `a` and `b`, `a == b` in Java is equivalent to `&a == &b` in C++ (objects at the same address), and Java's `a.equals(b)` is equivalent to C++'s `a == b` (the meaning depends on how `a`'s class implements `operator ==`). – Ted Hopp Sep 25 '13 at 02:51