0

The following code did not work. Can anyone tell me what's wrong with the following code. Logically it should work...

package assignments; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class IsPalindrome { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter a Word:"); StringBuffer sb1 = new StringBuffer(br.readLine()); StringBuffer sb2 = new StringBuffer(sb1); sb1.reverse(); if(sb2.equals(sb1)) System.out.println("Palindrome"); else System.out.println("Not a Palindrome"); } } 
6
  • 2
    You may want to use StringBuilder instead of StringBuffer (if you don't access it from multiple threads), as it's faster. Commented Jun 14, 2013 at 14:19
  • This might answer your query stackoverflow.com/questions/2012305/… Commented Jun 14, 2013 at 14:22
  • StringBuffer doesn't override equals() inherited from Object so contents in the StringBuffer aint compared just their reference. Commented Jun 14, 2013 at 14:29
  • If you don't already know, you should read the API documentation rather than making assumptions about the behavior of methods. The StringBuffer documentation says its equals method is inherited from Object. Commented Jun 14, 2013 at 14:31
  • Please don't use StringBuffer if you can use StringBuilder. This class has been deprecated for almost 10 years now. vanillajava.blogspot.com/2012/08/… Commented Jun 14, 2013 at 14:47

3 Answers 3

10

Try

sb1.toString().equals(sb2.toString()); 

because StringBuffer#toString method returns the String value of the data stored inside the buffer:

Returns a string representing the data in this sequence. A new String object is allocated and initialized to contain the character sequence currently represented by this object. This String is then returned. Subsequent changes to this sequence do not affect the contents of the String.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @darijan it worked but why can't we compare two stringbuffer objects directly?
Well, because equals method is not implemented in such way for StringBuffers. It returns true only when a StringBuffer is comapred to itself.
@user1377586 StringBuffer doesn't have its own equals implementation so the call ends up in Object's implementation that is reference based.
5

In StringBuffer class equals method is not overriden as in String class. In StringBuffer it just looks whether the references are the same. Therefore you first need to convert that to a String and then use equals method.

So Try

sb1.toString().equals(sb2.toString()); 

Comments

0

You can write

System.out.println("Enter a line:"); String line = br.readLine().replace(" ", ""); // palindromes can have spaces String reverse = new StringBuilder(sb1).reverse().toString(); if(line.equals(reverse)) System.out.print("Not a "); System.out.println("Palindrome"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.