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");
}
}
darijan
  • 9,725
  • 25
  • 38
Praveen Kumar
  • 51
  • 1
  • 2
  • 10
  • 2
    You may want to use `StringBuilder` instead of `StringBuffer` (if you don't access it from multiple threads), as it's faster. – Gabriel Negut Jun 14 '13 at 14:19
  • This might answer your query http://stackoverflow.com/questions/2012305/comparing-stringbuffer-content-with-equals – Gaurav Goyal Jun 14 '13 at 14:22
  • `StringBuffer` doesn't override `equals()` inherited from `Object` so contents in the `StringBuffer` aint compared just their reference. – Sello Jun 14 '13 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](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html) says its equals method is inherited from Object. – Patricia Shanahan Jun 14 '13 at 14:31
  • Please don't use StringBuffer if you can use StringBuilder. This class has been deprecated for almost 10 years now. http://vanillajava.blogspot.com/2012/08/java-memes-which-refuse-to-die.html – Peter Lawrey Jun 14 '13 at 14:47
  • Use equals() method of String class like below sb2.toString().equals(sb2.toString()) – Vishal Jun 05 '17 at 16:07

3 Answers3

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.

darijan
  • 9,725
  • 25
  • 38
  • Thanks @darijan it worked but why can't we compare two stringbuffer objects directly? – Praveen Kumar Jun 14 '13 at 14:18
  • 2
    Well, because `equals` method is not implemented in such way for `StringBuffer`s. It returns true only when a `StringBuffer` is comapred to itself. – darijan Jun 14 '13 at 14:22
  • 1
    @user1377586 `StringBuffer` doesn't have its own `equals` implementation so the call ends up in `Object`'s implementation that is reference based. – Fritz Jun 14 '13 at 14:24
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());
Dulanga
  • 1,326
  • 7
  • 15
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");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130