The moose likes Beginning Java and the fly likes Comparison failing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparison failing" Watch "Comparison failing" New topic

Comparison failing

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
This comparison is returning false even though I display the values of pageTag and tagVector.elementAt(tagVectorIndex), and they display exactly the same characters. What am I overlooking? I have pulled these bits of code from throughout a large program so hope I have included as much information as needed.
public static StringBuffer pageTag = null;
private static Vector tagVector;
public static StringBuffer buildTag (String pageIn, int indexIn){
StringBuffer tag = new StringBuffer();
int currentPlaceInPage = indexIn + 1;
while ( pageIn.charAt(currentPlaceInPage) != '>'){
tag.append( pageIn.toUpperCase().charAt(currentPlaceInPage));
indexInPage = currentPlaceInPage + 1;
return tag;
public static void buildVectorOfTags(String page1, int typeOfCompare){
indexInPage = 0;
tagVector = new Vector(10);
pageTag = null;
charactersInPage = page1.length();

while (indexInPage < charactersInPage - 1){
if (page1.charAt(indexInPage) == '<'){
pageTag = buildTag(page1, indexInPage);
} // end while
} // end buildVectorOfTags

Later, in the program I want to compare a tag from another page to one in the tagVector. When I look at these tags in debug or with println's, the values appear to be identical. Yet, my test is indicating they are not the same.
if (pageTag != tagVector.elementAt (tagVectorIndex) )
return false;
Thanks for any clarification.
Marcel de Jong
Ranch Hand

Joined: May 27, 2002
Posts: 54
instead of

try this

This should work.
When comparing String don't use == or !=, but use .equals.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Thanks for the help. It has also been pointed out that I need to cast the object from the Vector. I am now going to check, but I also read that StringBuffer (which pageTag is) does not have an equals method. Maybe I need to cast both sides of this comparison...
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
StringBuffer may not define its own equals method, but it does inherit one from Object. This won't do you much good as it only compares if two object references refer to the same object - not if the value/state of two objects are the same.
String does override the equals method from Object and StringBuffer does provide a method to create a String with the same characters that are in the StringBuffer.
Getting any ideas yet?
(bigger hint: take a look at the documentation for StringBuffer and note that the toString() method converts the StringBuffer to a String representing the data in the StringBuffer.)

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Comparison failing
jQuery in Action, 3rd edition