Search code examples
javapalindrome

Java - String index out of range


Getting this error for some reason and I don't know why. It occurs on line 70 where I have this code:

if(strNum.charAt(0) == strNum.charAt(strLength))

Here is the entire program: (Also any tips to clean this up is appreciated!)

/*
* Ryan Seipp
* This program will find the largest palindrome made from the product of two 3-digit numbers.
* A palindromic number reads the same both ways.
* 8/26/2015
*/
public class LargestPalindromeProduct
{
    public static void main(String[] args)
    {
        //Declare Variables
            int num1 = 999, num2 = 999;
            int product = num1 * num2;

        //Find Palindromic
            while(isPalindromic(product) == false)
            {
                for(num1 = 999; num1 > 0; num1--)
                {
                    for(num2 = 999; num2 > 0; num2--)
                    {
                        product = num1 * num2;

                        if(isPalindromic(product))
                        {
                            System.out.println(toString(product));
                            System.exit(0);
                        }
                    }
                }
            }
    }

    //This method will print the exiting statement
    public static String toString(int product)
    {
        String strProduct = ("The largest palindrome made from the product of two 3-digit numbers is: " + product);
        return strProduct;
    }

    //This method will find if an integer is palindromic
    public static boolean isPalindromic(int x)
    {
        //Convert x to char array
            String strNum = Integer.toString(x);

        //Get length of string
            int strLength = strNum.length();

        //Find if palindromic
            if(strLength == 5)
            {
                if(strNum.charAt(0) == strNum.charAt(strLength))
                {
                    if(strNum.charAt(1) == strNum.charAt(strLength - 1))
                        return true;

                    return true;
                }

                return false;
            }

            else
            {
                if(strNum.charAt(0) == strNum.charAt(strLength))
                {
                    if(strNum.charAt(1) == strNum.charAt(strLength - 1))
                    {
                        if(strNum.charAt(2) == strNum.charAt(strLength - 2))
                            return true;

                        return true;
                    }   

                    return true;
                }

                return false;
            }
    }
}

This is the error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:658)
at LargestPalindromeProduct.isPalindromic(LargestPalindromeProduct.java:70)
at LargestPalindromeProduct.main(LargestPalindromeProduct.java:16)

Solution

  • if string.length() is 6 that means String has characters from index 0-5 so accessing it at 6 can cause ArrayIndexOutOfBoundException.

    In your case,

    int strLength = strNum.length();//Available indexes 0,1,2,3,4
    if(strLength == 5) {//Because length is 5
    
        if(strNum.charAt(0) == strNum.charAt(strLength)) {//Here AIOB
        //Because you are trying this : strNum.charAt(5) which is invalid