File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparison failing

 
Mary Taylor
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
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){
indexInPage++;
if (page1.charAt(indexInPage) == '<'){
pageTag = buildTag(page1, indexInPage);
tagVector.addElement(pageTag);
}
} // 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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Betty,
instead of

try this

This should work.
When comparing String don't use == or !=, but use .equals.
Regards,
Marcel
 
Mary Taylor
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic