Hello again. :)
I am comparing two nodes (some of my other methods need this method). I am having a problem with this method because it returns false even though both nodes have the same content. What are the problems of this code? Any suggestions? This is the critical part of my code.

By the way, here's the code:

public boolean equals(Object o){		
		if(o instanceof Node){ //checks whether the object is an instance of Node
			System.out.println("Entering the first if statement"); // meaning o is an instance of Node
			if(((Node)o).elname == this.elname && ((Node)o).efname == this.efname && ((Node)o).work == this.work && ((Node)o).floorno == this.floorno){ //checking the contents of Nodes
				System.out.println("Entering the 2nd if and returned true"); //Contents are the same
				return true;
			}else{
				System.out.println("Entering the second if and returned false"); //contents are different
				return false;
			}
		}else{
			System.out.println("Entering the first if and returned false"); //O is not an instance of Node
			return false;
		}
	}

Recommended Answers

All 2 Replies

If some of those "things" are Strings, use String's equals method rather than == to compare them. (I'm assuming efname is a String.)

If some of those "things" are Strings, use String's equals method rather than == to compare them. (I'm assuming efname is a String.)

*cries for joy*
It works! Thank you very much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.