File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparison failing" Watch "Comparison failing" New topic
Author

Comparison failing

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
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

Joined: May 27, 2002
Posts: 54
Betty,
instead of

try this

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

Joined: Sep 11, 2000
Posts: 319
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

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: http://aspose.com/file-tools
 
subject: Comparison failing
 
Similar Threads
classic dictionary / hash table issue
How would I...
Appending to StringBuffer
Who said Java is easy?
as of release 5, 'enum' is a keyword...