ADAM KOZIAK
ADAM KOZIAK

Reputation: 11

Substring is causing a String index out of range error

I am writing a program that reverses 2 4 digit numbers, and I wrote a method that will do it. There are no errors in the project build, but I get "String index out of range: -3" when I try to run it. I am fairly new to programming and I have no idea what I did wrong.

Here's the code:

    public static void main(String[] args)
    {
        int num1 = 1;
        int num2 = 1;

        Scanner console = new Scanner(System.in);

        while (num1 <1000 || num1 > 9999)
        {
            System.out.println("Please enter a positive 4 digit number");
            num1 = console.nextInt();
        }

        String numString1 = Integer.toString(num1);

        while (num2 <1000 || num2 > 9999)
        {
            System.out.println("Please enter another positive 4 digit number");
            num2 = console.nextInt();
        }

        String numString2 = Integer.toString(num2);

        int numReverse1 = stringReverse(numString1);

        int numReverse2 = stringReverse(numString2);

        System.out.println(numReverse1 + numReverse2);

        System.out.println("The product of your 2 reversed numbers is: " + (numReverse1 * numReverse2));

    }

    public static int stringReverse (String numString)
    {
        String c1c2c3 = numString.substring(4);
        String c2c3c4 = numString.substring(1);

        String c1c2 = c1c2c3.substring(3);
        String c3c4 = c2c3c4.substring(1);

        String c1 = c1c2.substring(2);
        String c2 = c1c2.substring(1);
        String c3 = c3c4.substring(2);
        String c4 = c3c4.substring(1);

        String numStringReverse = c4 + c3 + c2 + c1;

        int reversedString = Integer.parseInt(numStringReverse);

        return reversedString;
    }
}

Upvotes: 1

Views: 991

Answers (2)

In the line:

String c1c2c3 = numString.substring(4);
String c1c2 = c1c2c3.substring(3);

You are trying to access a position that does not exists. Arrays in Java are started by 0 til " - 1".
Start index is inclusive, end index is exclusive in substring method.

A correct version for stringReverse` would be this:

public static int stringReverse (String numString)
{
    String c1c2c3 = numString.substring(0,3);
    String c2c3c4 = numString.substring(1,4);

    String c1c2 = c1c2c3.substring(0,2);
    String c3c4 = c2c3c4.substring(1,3);

    String c1 = c1c2.substring(0,1);
    String c2 = c1c2.substring(1,2);
    String c3 = c3c4.substring(0,1);
    String c4 = c3c4.substring(1,2);

    String numStringReverse = c4 + c3 + c2 + c1;

    int reversedString = Integer.parseInt(numStringReverse);

    return reversedString;
}

Also, try reading this answer.

Upvotes: 1

gil.fernandes
gil.fernandes

Reputation: 14611

There are perhaps more elegant ways to reverse a string in Java. If you want an easy alternative in Java look at the class java.lang.StringBuilder.

Check the documentation and look for the reverse method.

Upvotes: 0

Related Questions