0

I've written code for my CS class to identify if a user inputted string is a palindrome or not. I've gotten the code to work. However, whenever I execute the code I get this message:

"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:658)
    at palindrome.Palindrome.main(Palindrome.java:14)"

I think there's something wrong with my length and i but I'm not sure what. My code is below. I used aabbaa for the user inputted string.

package palindrome;

import java.util.Scanner;

public class Palindrome {

    public static void main(String[] args) {
        System.out.println ("Enter A String:");
        Scanner input = new Scanner (System.in);
        String s = input.nextLine();
        int length = s.length();
        int i = 0;
        while (length>= 0) {
            if (s.charAt(i) == s.charAt(length-1)) { 
                i++;
                length--;
                if (length == i) {
                    System.out.println ("Your string is a palindrome");
                    length = 0;
                }
            }
            else {
                System.out.println ("Your string is not a palindrome");
                length = 0;
            }
        }
    }
}
Perception
  • 79,279
  • 19
  • 185
  • 195
DewyT
  • 31
  • 2
  • 5
  • Just curious @downvoter, as to why this post was downvoted? – Jeel Shah Feb 13 '13 at 02:30
  • 1
    Nitpick - if it is throwing exceptions, you haven't got it to work. You've got it to compile, and in a sense you've got it to run. But it is NOT working in the normal sense of "working". – Stephen C Feb 13 '13 at 02:37

6 Answers6

2

When length equals 0, the first if-statement in your while loop tries to get s.charAt(0-1), i.e. s.charAt(-1), which is where the error occurs.

Perhaps try the following (not tested):

public static void main(String[] args) {
System.out.println ("Enter A String:");
Scanner input = new Scanner (System.in);
String s = input.nextLine();
int length = s.length();
int i = 0;
while (length > 0) {
    if (s.charAt(i) == s.charAt(length-1)) { 
        i++;
        length--;
        if (length < 1) {
            System.out.println ("Your string is a palindrome");
            break;
        }
    }
    else {
        System.out.println ("Your string is not a palindrome");
        break;
    }
}
kufudo
  • 2,803
  • 17
  • 19
  • Oh ok. That makes sense. I never considered that the length could equal zero. Thanks. Your explanation was very helpful. – DewyT Feb 13 '13 at 02:39
1

You write while(length >= 0) {} and in the main method you write length = 0; trying to the loop.

That should be while(length > 0){}

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
1

Just create a method like below and pass your string as argument to test whether string is a palindrome or not. Method will return true in case of string is palindrome

Idea is reverse your string and compare with original string if both are same then it is a palindrome.

public static boolean isPalindrome(String str) {
    return str.equals(new StringBuffer().append(str).reverse().toString());
}
Rais Alam
  • 6,970
  • 12
  • 53
  • 84
0

Your problem is that in every step you take two strides, but only decrease length by 1

So a trace: A user enters foooof.

length = 6
i = 0
f == f
i = 1
length = 5
o == o
i = 2
length = 4
o == o
i = 3
length = 3 (You've now passed yourself)
o == o
i = 4
length = 2
length-i = -2 =>     index out of range
Abraham P
  • 15,029
  • 13
  • 58
  • 126
0

When you iterate the while loop at that time value of length becomes negative, and array index starts from 0 thats why you getting the exception as StringIndexOutOfBoundsException.

You should try this, it will works fine.

import java.util.Scanner;

public class Palindrome
{
    public static void main(String[] args)
    {
        System.out.println("Enter A String:");
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        int length = s.length();
        int i = 0;
        while (length >= 0)
        {
            if (s.charAt(i) == s.charAt(length - 1))
            {
                i++;
                length--;
                if (length-1 == i)
                {
                    System.out.println("Your string is a palindrome");
                    length = 0;
                    break;
                }
            }
            else
            {
                System.out.println("Your string is not a palindrome");
                length = 0;
                break;
            }
        }
    }
}
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
parag.rane
  • 139
  • 1
  • 6
0

You should do some thing like this:

package palindrome;

import java.util.Scanner;

public class Palindrome {

    public static void main(String[] args) {
        System.out.println ("Enter A String:");
        Scanner input = new Scanner (System.in);
        String s = input.nextLine();
        if ( s.equals(new StringBuffer(s).reverse().toString()) ){
            System.out.println ("Your string is a palindrome");
        else
            System.out.println ("Your string is not a palindrome");
    }
}
Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38