Mathews Mathai
Mathews Mathai

Reputation: 1707

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.

Upvotes: 2

Views: 482

Answers (6)

Monroy
Monroy

Reputation: 430

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 + " ");
    }
}

Upvotes: 0

user4452436
user4452436

Reputation:

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]+" ");
        }
        }

}

Upvotes: 0

ifly6
ifly6

Reputation: 5331

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.

Upvotes: 2

Debosmit Ray
Debosmit Ray

Reputation: 5413

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

Upvotes: 1

SomeJavaGuy
SomeJavaGuy

Reputation: 7357

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'))

Upvotes: 6

shmosel
shmosel

Reputation: 50776

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.

Upvotes: 3

Related Questions