-3

I'm getting this error: java.lang.StringIndexOutOfBoundsException: String index out of range: 7

and I don't know what's causing the out of bounds exception. Here's my code:

public class LuckySeven {
    public int luckySevens(String input, int index, int sevens) {
        int curDigit = input.charAt(index) - 48;
        if (curDigit == 7) {
            if (index > 0 && (int) input.charAt(index - 1) - 48 == 7) {
                sevens += 14;
            }
            else sevens++;
        }
        if (index < input.length()) {
            luckySevens(input, index+1, sevens);
        }
        return sevens;
    }
}

I'm just running luckySevens("1087171", 0, 0) but it keeps breaking when it tries to repeat. Here's the error message:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
    at java.lang.String.charAt(String.java:658)
    at LuckySeven.luckySevens(LuckySeven.java:6)
    at LuckySeven.luckySevens(LuckySeven.java:14)
    at LuckySeven.luckySevens(LuckySeven.java:14)
    at LuckySeven.luckySevens(LuckySeven.java:14)
    at LuckySeven.luckySevens(LuckySeven.java:14)
    at LuckySeven.luckySevens(LuckySeven.java:14)
    at LuckySeven.luckySevens(LuckySeven.java:14)
    at LuckySeven.luckySevens(LuckySeven.java:14)
    at Tester.main(Tester.java:7)

I would really appreciate any help! I feel like it's some dumb thing I'm not catching. Thank you!

2 Answers2

0

The problem with your code is that you're referencing int curDigit = input.charAt(index) - 48;, while your if statement is working off of input.length().

What I mean by this, is that say the string "hello" is passed into your function. String.length() returns the length of the function, in this case 5. On the second-to-last cycle, index will be equal to four, and the function will run again with index equal to 4+1, or 5.

But because input[5] would try to reference the sixth character of the string, which does not exist, you get a string IoB error.

To remedy this, try replacing

if (index < input.length()) {
    luckySevens(input, index+1, sevens);
}

with

if(index < input.length() - 1) {
    luckySevens(input, index+1, sevens)l
}

or consider using a for loop instead. Hope this helps :D

0

Start by looking at the exception thrown,

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
    at java.lang.String.charAt(String.java:658)

String index out of range : 7 indicates that you are requesting index 7 and that it is out of bounds. Since your string 1087171 is 7 characters long, it only has indices 0-6 (0, 1, 2, 3, 4, 5, 6); there is no 7th index.

If we continue to look at the rest of the exception, we can further determine what is happening:

at LuckySeven.luckySevens(LuckySeven.java:6)
at LuckySeven.luckySevens(LuckySeven.java:14)
at LuckySeven.luckySevens(LuckySeven.java:14)
at LuckySeven.luckySevens(LuckySeven.java:14)
at LuckySeven.luckySevens(LuckySeven.java:14)
at LuckySeven.luckySevens(LuckySeven.java:14)
at LuckySeven.luckySevens(LuckySeven.java:14)
at LuckySeven.luckySevens(LuckySeven.java:14)

the line at LuckySeven.luckySevens(LuckySeven.java:6) is the expression that caused the error to be thrown. In this case, it is int curDigit = input.charAt(index) - 48;. The next 7 lines of at LuckySeven.luckySevens(LuckySeven.java:14) are the recursive method invocations for the first 7 characters of the string. So we can tell that it worked through the first 7 characters, but then called the luckySevens method an 8th time.

As another user already posted, change the block

    if (index < input.length()) {
        luckySevens(input, index+1, sevens);
    }

to

    if (index < input.length()-1) {
        luckySevens(input, index+1, sevens);
    }

and it will stop at the 7th character.