1

This code always returns false. What might be the problem ?

  public class S18_Palindrome {
      public static void main(String[] args) {
        String myString = new String("malayalam");
        if (myString.equals(new StringBuilder(myString).reverse())) {
          System.out.println("true");
        } else {
          System.out.println("false");
        }
      }
    }
0

3 Answers 3

6

The problem is that StringBuilder(myString).reverse() will return a StringBuilder object you need to get the string value out of it.

new StringBuilder(myString).reverse().toString()
Sign up to request clarification or add additional context in comments.

Comments

1

Got it :)

I had to convert the StringBuilder's value toString()

if (myString.equals(new StringBuilder(myString).reverse().toString()))

instead of

if (myString.equals(new StringBuilder(myString).reverse()))

Comments

-1

Try this..

String s = "responses";
StringBuilder builder = new StringBuilder(s);
System.out.println(builder.reverse());

2 Comments

it is just example of check reverse string.
But he asked why myString.equals(new StringBuilder(myString).reverse()) always returns false..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.