简体   繁体   中英

Why is this program giving me an array index out of bounds exception?

static char characters[] = { 'a', 'b', ' ', 'c', ' ', 'e', 'f', ' ' };

public static void main(String args[]) {

    for (int i = 0; i < characters.length; i++) {
        int j = i + 1;

        if (characters[i] == ' ') {
            while (j < characters.length && characters[j] == ' ' || characters[j] == '\u0000') {
                j++;  // increment j till a non-space char is found
            }
        }
    }

    for (char character : characters) {
        System.out.print(character + " ");
    }
}

Eclipse shows the following error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at loopchecker.main(loopchecker.java:13)

Line 13 is the while loop . But I am already checking if j's value is less than the length or not.

It´s because of the or operator.

The following condition reads like that

(j < characters.length && characters[j] == ' ') || characters[j] == '\u0000'

so you allways execute the or condition due to the precedence of the and operator above the or operator which causes the ArrayOutOfBoundException. You might want to include braces in your while condition.

while (j < characters.length && (characters[j] == ' ' || characters[j] == '\u0000'))

You need to add some parentheses to your while statement:

while (j<characters.length && (characters[j] == ' ' || characters[j] == '\u0000'))

Otherwise, Java's order of operations sees it as

while ((j<characters.length && characters[j] == ' ') || characters[j] == '\u0000')

which means characters[j] will still be evaluated if the first portion is false.

Given that in the comment, you want to say //increment j till a non-space char is found , I would say that you should get rid of:

while (j<characters.length && characters[j] == ' ' || characters[j] == '\u0000')

Which is causing your error due to the problem with the || being evaluated after the && and replace it with:

while (j<characters.length && Character.isWhitespace(characters[j]))

This solves your problem along with dealing with the whitespace issue much more conveniently.

The characters[j] == '\' part doesn't have a check to ensure that j is less than the length. Put something like j < characters.length && (.. || ..)

It is because of the OR operator . Below is the complete solution .

package com.java;

public class ArraySample 
{
    static char characters[]={'a','b',' ','c',' ','e','f',' '};
    public static void main (String args[])
    {
        for (int i = 0; i < characters.length; i++) {
            int j = i + 1;
            if (characters[i] == ' ') {
                while (j < characters.length && (characters[j] == ' ' || characters[j] == '\u0000')) {

                    j++;  //increment j till a non-space char is found

                }

            }
        }
        for(int i = 0; i< characters.length; i++)
        {
            System.out.print(characters[i]+" ");
        }
        }

}
static char characters[] = { 'a', 'b', ' ', 'c', ' ', 'e', 'f', 's' };

public static void main(String args[]) {

    for (int i = 0; i < characters.length; i++) {
        int j = i - 1 ; // The problem was here J was surpassing the characters length

        if (characters[i] == ' ') {
            while (j < characters.length && characters[j] == ' ' || characters[j] == '\u0000') {
                j++;  // increment j till a non-space char is found
            }
        }
    }

    for (char character : characters) {
        System.out.print(character + " ");
    }
}

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