• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to compare strings  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello I'm having trouble with this code. Its supposed to notify the user if they have a palindrome, but keeps returning true even when I type, "hello". Can someone help notify me why, the only reason i could think of is that it is not comparing characters but I assumed that's what the .equals is for or am I mistaken?

 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your reasoning is spot on - it is not comparing characters. And no, that is not what "equals" is for - equals tests whether the object identities are the same (which they are not, given that Y and Z are two different objects). But the StringBuilder class has a method that turns their character content into String objects - which, confusingly, can be compared using equals.

By the way, I edited your post to UseCodeTags, which makes the code much easier to read. Please do that in the future if you post code of any length.
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to compare them then?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. As Ulf said, convert them to Strings and theon use equals(). One of them doesn't even need converting because it starts out as String - just use that.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, using the toString method.

The problem is that line 18 does not do what you think it does. It causes Y to be reversed, and then a reference to the newly reversed Y to be assigned to Z. So both variables point to the same StringBuilder.
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh thanks question my book says to use this
(Z.toString().equals(Y.toString()))


to compare content, why doesnt this work when i try it
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that reverse(), which you are using in line 18, changes the content of the StringBuilder that you call it on. So, in this case, it will change the content of Y. Then you assign Z to the return value of Y.reverse(). That method just returns a reference to Y.

So, after line 18, Y and Z will be pointing to the exact same StringBuilder object, so Y.equals(z) or Y.toString().equals(Z.toString()) will always return true.

Change line 18, remove the "Z =" in front of Y.reverse().
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and in line 17, Z would need to be initialized differently. Or you can just use X, and not have Z at all, like Matthew suggested.
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its working perfectly now
Thanks to all of you
 
Greenhorn
Posts: 15
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals() method is used to check whether two strings arrays values are equals or not and equal to operator (==) in case of String is used to check whether two strings references are equal or not. Equal to operator and equals() method has big difference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!