-5
class palindrome
{
    public static void main(String args[])
    {
        String s1=new String();
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter the string:");
        s1=sc.nextLine();
        StringBuffer s2=new StringBuffer(s1);
        s2.reverse().toString();    
        if(s1.equals(s2.toString()))
            System.out.println("Given String is palindrome");
        else
            System.out.println("Given String is not palindrome");
    }
}

This is my code to check whether a string is palindrome or not.

I get the correct output, but I have 2 questions:

1) Why cannot we use toString like s1.toString()

2) If I write if(s1.equals(s2)) instead of just using an if condition is skipped and directly else condition is ran in output, why so?

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 1
    1: s1 already is a string. You can still use s1.toString() but thats pretty redundant. 2. s2 is not a String. A String (s1) cannot be equal to something that is not a String (s2), therefor you have to convert s2 first. – OH GOD SPIDERS Oct 04 '17 at 10:47
  • 1
    I tried to edit this question but still didn't really understand the second question :-| – achAmháin Oct 04 '17 at 10:48
  • 1
    @JamesMacca but if you read the question, it is working, the problem is not about the Palindrom but about `String` and `StringBuffer`. – AxelH Oct 04 '17 at 11:07
  • "_condition is skipped_", no, the condition isn't true so the block of statement is not executed, instead it execute the `else` block. It is complicated to understand if you don't use the correct (or a close) terminology. – AxelH Oct 04 '17 at 11:09
  • 1
    @JamesMacca not my question, so I don't need anything. And the edit is mostly formatting. The content was there, especially "_I get the correct output_". (end of the chat) – AxelH Oct 04 '17 at 11:11
  • I beleive the issue is infact because you need the if statement to be: if(s1.equals(s2.reverse().toString()). You called s2.reverse().toString(), above however did not save it anywhere. – James McNee Oct 04 '17 at 11:13
  • Possible duplicate of [String, StringBuffer, and StringBuilder](https://stackoverflow.com/questions/2971315/string-stringbuffer-and-stringbuilder) – vaquar khan Oct 04 '17 at 11:56

2 Answers2

0

In your case s1 is a String and s2 is StringBuffer.

The Object class's equals method returns true if this instance is equal to the argument. That is why you got s1.equals(s2) as false.

But If you are converting the s2 to a String then that will give true value. Because string builder's toString() method will return the String value. So please read the difference between String and StringBuffer and StringBuilder.

and toString(). equals() and hashCode() methods of Object class.

Vishnu KR
  • 718
  • 1
  • 8
  • 22
0

First off, don't create a new String object using new String(). Strings are special objects and you should not instantiate them with the new keyword but rather make use of the String constant pool as follows:

String s1 = "value"; // with value assignment
String s2; // without value assignment

Now that this is out of the way, let me get to your question.

[1] You can use .toString() on a String object, but why would you? Makes no sense to "convert" a character sequence to... a character sequence.

[2] If I understand your question right, during testing you most likely typed in a word that starts with a capital letter, so if you type in Rotor and your program reverses it, it ends up as rotoR, and these two Strings are not equal. String#toLowerCase eliminates that problem.

I've improved your code:

String text;

Scanner sc = new Scanner(System.in);
System.out.println("Enter your message:");
text = sc.nextLine().toLowerCase();

String reversedText = new StringBuilder(text).reverse().toString();

if (text.equals(reversedText)) {
    System.out.println("Congratulations, it's a palindrome!");
} else {
    System.out.println("Just another plain normal String...");
}
Mikusch
  • 125
  • 1
  • 8