简体   繁体   中英

ArrayIndexOutOfBoundsException when iterating through an array

public static int calcScore(char[] inputChars) {
    int gameScore = 0;
    for(int i = 0; i < inputChars.length; i++) {
        System.out.println(inputChars[i]);
        if(inputChars[i] == 'X') {
            gameScore += 10;
            gameScore += getPointsStrikeSpare(i, inputChars);
        } else if (inputChars[i] == '-') {
            gameScore += 0;
        } else if (inputChars[i] == '/') {
            gameScore += 10;
            gameScore += getPointsStrikeSpare(i, inputChars);
        } else {
            gameScore += Character.getNumericValue(inputChars[i]);
        }
    }
    return gameScore;
}

So my problem is that I want to itterate through the inputChars array (created using .toCharArray() ) and it works fine but it cannot process the last character.

Here is the content of the input variable: "X-/X5-8/9-X811-4/X".

inputChars is input.toCharArray().

Here is the output when I run the code :

X
-
/
X
5
-
8
/
9
-
X
8
1
1
-
4
/
X
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
    at betterCalculateScore.getPointsStrikeSpare(betterCalculateScore.java:37)
    at betterCalculateScore.calcScore(betterCalculateScore.java:20)
    at betterCalculateScore.main(betterCalculateScore.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

I have tried using i < inputChars.length - 1 but then it just doesn't count the last character.

Here is the content of getPointsStrikeSpare :

    public static int getPointsStrikeSpare(int i, char[] inputChars) {
    int points = 0;

    if(inputChars[i] == 'X') {
        if(inputChars[i+1] == '-') {
            points += 0;
        } else if (inputChars[i+1]== '/') {
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+1]);
        }
        if(inputChars[i+2] == '-') {
            points += 0;
        } else if (inputChars[i+2]== '/') {
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+2]);
        }
    } else {
        if(inputChars[i+1] == '-') {
            points += 0;
        } else if (inputChars[i+1]== '/') {
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+1]);
        }
    }
    return points;
}

At various places in the public static int getPointsStrikeSpare(int i, char[] inputChars) method, elements are being accessed which do not belong to the array length boundary. Consider the code blocks:

    if(inputChars[i+1] == '-') 

as well as

    if(inputChars[i+2] == '-') {

In the case of i == inputChars.length - 1, both i+1 as well as i+2 exceed the array length, hence the java.lang.ArrayIndexOutOfBoundsException exception.

In general, if the value of 'i' is equal to or less than the length of (array_size - 1), a[i] would be valid.

However, in this method, public static int getPointsStrikeSpare(int i, char[] inputChars) one can see that a[i+1] and a[i+2] end up getting queried in different code paths in the if-else structure. So, if your 'i' index already reached the end of the array, this is going to be Out of Bounds.

Ex: X-/X5-8/9-X811-4/X The size of this is 18. So, the legal values to refer are from 0-17 ie, a[0] to a[17], since indexing in array starts from 0. Towards the end of the program, index counter 'i' becomes 17.

if(inputChars[i] == 'X') {               // VALID
        if(inputChars[i+1] == '-') {     // INVALID
            points += 0;
        } else if (inputChars[i+1]== '/') {  // INVALID
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+1]); // INVALID
        }
        if(inputChars[i+2] == '-') { // INVALID

You get the general idea here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM