user1810925
user1810925

Reputation: 63

Array index out of bounds Error

I've been having some trouble with this code I made up, in that I've been getting an ArrayIndexOutofBounds exception error and I have no idea what is going on. What I want my code to produce is a char array of numbers.

Here's the code:

public class testing {

    static int k = 2;

    public static void main(String args[]) {

        int[] numArr = new int[] { 15, 18, 21 };

        createChar(numArr, k);
    }

    public static char[] createChar(int numArr[], int k) {
        char[] store = new char[k - 1];

        for (int i = 0; i < k; i++) {
            int divide = numArr[i] / 4;

            int numStore = divide % 4;
            charN(numStore, store, k);
        }
        return store;
    }

    public static char[] charN(int numStore, char store[], int k) {
        for (int i = 0; i < k; i++) {
            if (k == 0) {
            } else {
                store[k - 1] = (char) numStore;
                k = k - 1;
            }
        }
        System.out.print(store);
        return store;
    }
}

Thanks a lot!

Upvotes: 0

Views: 255

Answers (3)

Rohit Jain
Rohit Jain

Reputation: 213193

For value k = 2, the below statement: -

char[] store = new char[k - 1];

creates an array of size 1. So, you can only access its 0th index.

But, further in the charN method, you are accessing it's 1st index at: -

store[k - 1] = (char) numStore;  // Since k = 2, k - 1 = 1.

Change your array creation to: -

char[] store = new char[k];

Furthermore, I don't understand why you took k = 2 on first place. May be you meant it to be used as index, in which case, your array creation would be of size k + 1. And accordingly, your for loop will iterate till k + 1, rather than k.

Also, I don't understand the role of your charN method. In fact, tt seems strange that first you are iterating over your array in createChar method, and then passing each element to charN method. And then there also you are iterating over the char array, assigning the same value to multiple index. Apart from that, you are decrementing k and incrementing i at the same time in the loop. That is really strange. And at the end, you are returning your array from both of your methods, but are not using the return value at all.

It's quite hard to understand what you want to do. But you should consider all the points I stated in the previous paragraph. For each step there, think of why you want to do this? Is there any alternative, which might be easy? Do, I really need to methods with 2 iteration here?

I suggest you to take a look at your design once again. The solution which I posted above might solve your compiler error, but there is some problem with your logic. You need to take care of that.

Upvotes: 1

sr01853
sr01853

Reputation: 6121

As the error suggests, it is due to access outside the bounds of the array. ie. some bad index access on an array.

char[] store = new char[k - 1];

You are creating a store character array of size k-1

and passing k as size to charN()

charN(numStore, store, k);

To resolve your problem. change store declaration as below

char[] store = new char[k];

Also, in class Testing, k must be 3, not 2. where k is the size of the array.

static int k = 3;

When size of the array is k, the indices of array run from 0 to k-1

Upvotes: 4

user1760178
user1760178

Reputation: 6657

As per your post the statement

char[] store = new char[k - 1];   

results in a char array of size 1. So in charN method when you try to access the

store[k - 1] = (char) numStore;

you are trying to access store[1] as 'k' is 2 here. Which is wrong. because with store[1] you are trying to access the second element in the array where as the array size is only 1.

Upvotes: 0

Related Questions