0

Could someone please point out the mistake in my program?

The output is always: "it is not a palindrome"

String newstr="";
for(int j=length-1;j>0;j--)
{
    char m=str.charAt(j);
    newstr=newstr+m;
}
if(newstr.equals(str) )
    System.out.println("it is a palindrome");
else
    System.out.println("it is not a palindrome");

thanks in advance:)

aqua
  • 719
  • 4
  • 11
  • 17
  • Please don't edit your question to silently incorporate all fixes given in the answers. This makes it impossible to see what was actually asked. Also -- your updated code should work, so there is no more question to answer. – creinig May 22 '13 at 11:41
  • @creinig The updated code fails, see [Shah's answer](http://stackoverflow.com/a/16690648/2040040) – johnchen902 May 22 '13 at 11:43
  • Sorry,and no,it still doesn't work. – aqua May 22 '13 at 11:43
  • @johnchen902: Ah right, missed that one. So only half of my comment still stands :) – creinig May 22 '13 at 11:51

6 Answers6

3

First of all, use String.equals() to compare Strings instead of ==.

if(newstr.equals(str))

Also, string index start with 0, so you need:

for(int j=length-1;j>=0;j--)

Both fixes should work.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
3
if(newstr==str)

should probably be

if ( newstr.equals( str ) )

And do read up on object comparison. You're effectively comparing two pointers, not the string contents.

Of course, with Java 5 and onwards you could just do

new StringBuilder( str ).reverse().toString().equals( str );

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
2

index of array starts from 0. I guess you should use

for(int j=length-1;j>=0;j--)

instead of

for(int j=length-1;j>0;j--)
Anand Shah
  • 611
  • 7
  • 14
1

== operator check whether both the reference point to the same object or not. .equals() method will actually check the content of the Strings.

So your code must be

    if(newstr.equals(str))
    System.out.println("it is a palindrome");
    else
    System.out.println("it is not a palindrome");
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

==tests for reference equality.

.equals() tests for value equality.

Consequently, if you actually want to test whether two strings have the same value you should use .equals() (except in a few situations where you can guarantee that two strings with the same value will be represented by the same object

This will be the final code. read about string comparison in java

  String newstr="";

   for(int j=length-1;j>0;j--)
   {
       char m=str.charAt(j);
       newstr=newstr+m;
    }
    if(newstr.equals( str ) )
    System.out.println("it is a palindrome");
    else
    System.out.println("it is not a palindrome");
Community
  • 1
  • 1
Zigma
  • 529
  • 6
  • 37
0
public static boolean isPaliandrome(String str) {
        StringBuilder lettersBuff = new StringBuilder(str);
        String str_inverse = lettersBuff.reverse().toString();
        char[] charArrayInverse = str_inverse.toCharArray();
        boolean isPaliandrome = false;
        String caracInverseConverted = new String(charArrayInverse);
        if (str.equals(caracInverseConverted)) {
            isPaliandrome = true;
        }
        return isPaliandrome;
    }