atomic_variable
atomic_variable

Reputation: 124

array index out of bounds exception for no reason

I am getting array index out of bounds exception at the last line of the prog, output[1][1] generates such an exception there. But the same thing remained normal in the previous loop.

{  
    int ict=66,total_slots=12,h1=15,index_count=65;
    String output[][] = new String[ict][total_slots+1];

    for (int x = 0; x < (ict); x++)
        output[x][0] = Integer.toString(x);
    for (int y = 0; y < (total_slots + 1); y++)
        output[0][y] = Integer.toString(y);
    for (int x = 1; x <= (index_count); x++ )
        for (int y = 1; y <= (total_slots); y++)
            output[x][y] = "n";

    for (int x=1; x <= h1; x++) {
        output[x][1]="y";//exception occurs here
        limit[x]++;
    }
}

Upvotes: 0

Views: 893

Answers (3)

glee8e
glee8e

Reputation: 6419

I am getting array index out of bounds exception at the last line of the prog, output[1][1] generates such an exception there.

Recheck the console output. You must have mistaken the erroring line, since there is no way to generate such an exception. The problem most probably lies in the limit[], which we don't know how it's declared.

Btw, the last line is limit[x]++ ;)

Upvotes: 1

ForeverStudent
ForeverStudent

Reputation: 2537

Remember when you declare an array like String output[][]=new String[5][5]; then output[4][4] is the last element you can access since array indexes start at 0

you have declared your array as:

String output[][] = new String[ict][total_slots+1];

in your for loop you have

for( int x=1; x<=(ict); x++ )
    for( int y=1; y<=(total_slots); y++)
         output[x][y]="n";

On the very last iteration of the outer loop and the very first iteration of the inner loop, you are trying to access output[ict][0] which is what throws the exception. Remember that since 0 is the first index, ict - 1 will be the last valid index for the first dimension.

Try this:

String output[][] = new String[ict][total_slots]; //this is cleaner

for( int x=0; x<ict; x++ )
    for( int y=0; y<total_slots; y++)
         output[x][y]="n";

This way the last iteration of the outer loop would only go up to ict - 1

edit: It seems you have edited the question significantly. Please do not edit your question in the future in response to answers, because this will only confuse new readers.

As your code stands right now, the only error (compile time no less, irrelevant to exceptions) is the fact that limit[x]++; is invalid because limit has not been declared in this scope. otherwise the code is legal and runs fine.

Upvotes: 1

Harshavardhan Konakanchi
Harshavardhan Konakanchi

Reputation: 4294

    {  
        int ict=66,total_slots=12,h1=15,index_count=65;
..................................................
..................................................
        for (int x=1; x <= h1; x++) {
                output[x][1]="y";//exception occurs here
                limit[x]++;
            }
        }

At the last for loop, you recieve ArryIndexOutOfBoundsException , while trying to access the element output[13][1] cause your array width is only 13 i.e max index will be 12 but not more than that

Use total_slots instead of h1

Upvotes: 1

Related Questions