0
package palindrome;
import java.util.Scanner;
public class Palindrome 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the word = > ");
        String word = input.nextLine();
        int flag = 0;
        int x = word.length();
        int i = 0;
        int j = x;
        for(; i<=x/2 && j>=x/2; i++,j--)
        {
            if(word.charAt(i)!=word.charAt(j))
            {
                flag = 1;
                break;
            }
        }
        if(flag==0)
        {
            System.out.printf("The word '%s' is a palindrome", word);
        }
        else 
            System.out.printf("The word '%s' is not a palindrome", word);
    }


}

Output scream error:

Enter the word = > madam

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at palindrome.Palindrome.main(Palindrome.java:16)
Andy Turner
  • 137,514
  • 11
  • 162
  • 243

8 Answers8

2
int j = x;

should be:

int j = x-1;
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
1

You have to set j's initial value to length-1

import java.util.Scanner;
public class Scratch 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the word = > ");
        String word = input.nextLine();
        int flag = 0;
        int x = word.length()-1;
        int i = 0;
        int j = x;
        for(; i<=x/2 && j>=x/2; i++,j--)
        {
            if(word.charAt(i)!=word.charAt(j))
            {
                flag = 1;
                break;
            }

        }
        if(flag==0)
        {
            System.out.printf("The word '%s' is a palindrome", word);
        }
        else 
            System.out.printf("The word '%s' is not a palindrome", word);
    }


}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
emrhzc
  • 1,347
  • 11
  • 19
  • It would be better to set `j`'s value to `x - 1`, rather than setting `x`'s value to `word.length()-1`. Then again, changing the loop guard to `i < j` would be easier, and would eliminate the need for `x`. – Andy Turner Jun 08 '16 at 13:22
  • Given the code, one simple fixes with minimum change. I mean look at the code man, would you use that much variable for such a trivial task? – emrhzc Jun 08 '16 at 13:33
0

Index is start from 0 in array.

package palindrome;
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the word = > ");
    String word = input.nextLine();
    int flag = 0;
    int x = word.length();
    int i = 0;
    int j = x-1;
    for(; i<=x/2 && j>=x/2; i++,j--)
    {
        System.out.println(i+" " +j );
        if(word.charAt(i)!=word.charAt(j))
        {
            flag = 1;
            break;
        }
    }
    if(flag==0)
    {
        System.out.printf("The word '%s' is a palindrome", word);
    }
    else
        System.out.printf("The word '%s' is not a palindrome", word);
}

}

0

you just forget to reduce the length of the word by 1: remember index of Strings and Arrays in Java begins from 0 so the length should always be minus 1 since it will not be reached. example if the length of your words is 4 you can get up to index 3 that is 0 to 3 makes total length of 4.

int x = word.length()-1;
    int i = 0;
    int j = x;
    for (; i <= x / 2 && j >= x / 2; i++, j--) {
        if (word.charAt(i) != word.charAt(j)) {
            flag = 1;
            break;
        }
    }
Seek Addo
  • 1,871
  • 2
  • 18
  • 30
0

Just to clarify the other answers:
A string is an array of chars, so if you write "madam"

[m][a][d][a][m] -> is 5 letters, so its length is 5
[0][1][2][3][4] -> but the indexes start with 0, so the last one is 4

Hence, the x=word.length()-1

Vale
  • 1,104
  • 1
  • 10
  • 29
0

I got the mistake in your code.

"value of j = x;" // is wrong As String is a character array it starts with 0 to string.length-1.

thus int j = x-1 ; // will do right

0

My suggestion is that you should have a seprate method to check for palindrome, This way you make your code clean and quality code.You can use below method to check for palindrome:

public static void main(String[] args) 
{
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the word = > ");
        String word = input.nextLine();

       if(isPalindrome(word))
       {
           System.out.printf("The word '%s' is a palindrome", word);
       }
      else 
          System.out.printf("The word '%s' is not a palindrome", word);
}


private static boolean isPalindrome(String input) {
        if(input == null || input.isEmpty()) {
            return false;
        }
        for(int i=0,j=input.length() -1; i > j; i++,j-- ) {
            if(input.charAt(i) != input.charAt(j)) {
                return false;
            }
        }
        return true;
}
pbajpai
  • 1,303
  • 1
  • 9
  • 24
-2

I suppose this happens because of the result of 5 divided to 2. Try keeping the middle length first as integer.

Bogdan Mates
  • 520
  • 4
  • 8