The char array has 147 elements, and there's a number after each letter.

Each letter activates a method, followed by 1-3 parameters (numbers).

However, inside the for loop, I am getting a -1 value and sometimes random large numbers (all my numbers are positive and less than 12).

If I manually call any method outside the for loop for example:

fridge[Character.getNumericValue(arr[0].insertItem(Character.getNumericValue(arr[1]);, everything works fine and the value which is at arr[1] (which is 1) is stored at this.fridgeItems (an int[] array).

Why is this happening? I know it's a problem with either the switch statement or the for loop but I can't really tell.

网友答案:

Check your indices. In your code you are accessing elements from arr by i + 1 and i + 2 index. If your loop iterates more than n - 2 times, you will have IndexOutOfBoundException.

By the way, you need to increment i in your switch statement to skip "used" characters from the input array.

网友答案:

Your code is "not increment i correctly", because there is no code in the switch statement to actually increment i.