rsdev
rsdev

Reputation: 140

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)

Upvotes: 2

Views: 723

Answers (4)

Luke Melaia
Luke Melaia

Reputation: 1468

The thing to remember about a string is its an array of characters that range from 0 to n, so str.length() is returning how many characters are in the string, not what the last position is. Getting the last element is to use str.length() - 1.

Upvotes: 1

Akash Thakare
Akash Thakare

Reputation: 22972

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

Upvotes: 2

Iman Rosstin
Iman Rosstin

Reputation: 2345

change the line to

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

and it will work. the last element of arrays in java is sizeOfArray-1

do not forget to accept the answer if it worked.

Good luck

Iman

Upvotes: 2

Warren Dew
Warren Dew

Reputation: 8928

The legal indices for a String go from 0 to String.length() - 1, so strLength, which is equal to strNum.length(), is an illegal index.

You need to replace strLength with strLength - 1, replace strLength - 1 with strLength - 2, etc.

Upvotes: 0

Related Questions