-1

while studying Array part in Java, I ran into this problem, and I searched about the error message,

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
        at Main.main(Main.java:12)"

and read some articles about it.

I got that the index start from 0 so it will end in n-1(n is the allocated size).But I still don't catch the problem in my code. It will mean a lot to me if you help me with it since array is kind of confusing part..

I need to return output :

1
3
5
7
9
2
4
6
8
10

And this is what I wrote.

import java.io.*;
class Main {
    public static void main(String[] args) throws Exception {
    int [] oddArray = new int[5];
    int [] evenArray = new int[5];

    int k = 0;

    do {
        k++;

        oddArray[k] = k + 1;

        evenArray[k] = k + 2;


    }while(k <= 10);


    for(int j = 0 ; j < 5 ; j++) {
        System.out.println(oddArray);
    }

    for(int j = 0 ; j < 5 ; j++) {
        System.out.println(evenArray);
        }
    }
}
Srinivasan Sekar
  • 2,049
  • 13
  • 22
Alex Jang
  • 61
  • 2
  • 5
  • 1
    You allocate five "slots". You iterate `k` from 0-10. 10 is greater than 5. – Dave Newton Jun 06 '19 at 13:07
  • Both arrays have a length of 5, so the highest index is 4. You start with index 1 (wrong, should be 0) and let it increase up to 10? How do you expect that to work? – Fildor Jun 06 '19 at 13:07
  • Have you looked at the line where the error occurs? What do you think `while(k <= 10)` does and what values it allows? Go through the loop step by step and think what values `k` will get over time and whether it is ok or not. – glglgl Jun 06 '19 at 13:07
  • your array size is 5. your "k" will go up to 10. – ColonD Jun 06 '19 at 13:07
  • I made it <= 10 since it should show odd and even number up to 10. So it starts from1 to 10. that's wrong? it doesn't relate with that? – Alex Jang Jun 06 '19 at 13:35
  • It doesn't work even if I corrected it to k < 5 what should I correct? – Alex Jang Jun 06 '19 at 13:38
  • What you need to understand is : Here `oddArray[k] = k + 1;` if k=0, then oddArray[0] = 1 , that's how that works. `k++` is short for `k = k+1;`, so if you do that first, you skip setting index 0 and go on to set oddArray[1] = 2 ... – Fildor Jun 06 '19 at 13:43

2 Answers2

1

Note that you are incrementing k BEFORE using it the first time with the "k++;" line. So when you are assigning values to oddArray and evenArray, your index starts at 1 and goes to 5 which is out of bounds.

You also have and error in your while loop. You are checking for 10 where you should be checking for 5.

while(k < 5);

Move the k++ to AFTER the assignments, fix the while loop checking, and it should work.

jwh20
  • 646
  • 1
  • 5
  • 15
0

You need to separate k and array index. Otherwise, when k=5 you're adding 6 to oddArray[5], which is out of bounds.

I'd say something like this

        final int ARR_SIZE = 5;
        int [] oddArray = new int[ARR_SIZE];
        int [] evenArray = new int[ARR_SIZE];

        int k = 0, index = 0;

        do {
            oddArray[index] = k + 1;
            evenArray[index] = k + 2;
            index++;
            k+=2;
        }while(index < ARR_SIZE);


        for(int j = 0 ; j < ARR_SIZE ; j++) {
            System.out.println( oddArray[j]);
        }

        for(int j = 0 ; j < ARR_SIZE ; j++) {
            System.out.println(evenArray[j]);
        }
John Smith
  • 427
  • 6
  • 17
  • If this was only one array, I'd advise to iterate to array.length. Here we have two arrays, so I would advise to use a Size-Constant, which is used for creating both arrays and then iterating to `index < ARR_SIZE`, where ARR_SIZE is said constant. – Fildor Jun 06 '19 at 13:19
  • Can't argue on that, will update code. – John Smith Jun 06 '19 at 13:34