1

Trying to find the sum of all even numbers and product of all odd numbers in a double dimensional array.

Why am I getting the following out of bounds exception error ?

Exception java. lang. Array Index Out Of Bounds Exception

While running the code this exception comes for line if(m[i][j]%2==0)

photo of code

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
John Doe
  • 29
  • 4
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – OH GOD SPIDERS Jun 19 '19 at 12:31
  • 3
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Post code and markup and such **as text**, not as a *picture* of text. Why: http://meta.stackoverflow.com/q/285551/157247 – T.J. Crowder Jun 19 '19 at 12:31
  • Because when i=1 and j=4, the index doesn't exist in your array. – Proyag Jun 19 '19 at 12:45

3 Answers3

2

The exact cause of your error is that your 2D array is actually a jagged array, meaning that not every row contains the same number of elements. In fact, the second row only contains three elements, so when you when the following if check:

if (m[i][j]%2 == 0)

you get an out of bounds exception for i=1 and j=3.

You should either make the 2D array non-jagged, or instead use this for loop:

for (int i=0; i < 4; ++i) {
    for (int j=0; j < m[i].length; ++j) {
            if (m[i][j]%2 == 0) {
                s += m[i][j];
            }
            else {
                r *= m[i][j];
            }
        }
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • for some reason I am getting the result zero for variable r , is there any solution for this ? – John Doe Jun 19 '19 at 12:53
  • @JohnDoe that's a different problem. The reason is that you're initializing `r = 0`, so no matter what value you multiply it by, the result will always be `0`. i.e.: `0 * 23 = 0`, right? – lealceldeiro Jun 19 '19 at 12:59
  • the program is now working successfully thanks for your timely help – John Doe Jun 19 '19 at 13:11
1

Your m array's element at index 1 is missing a fourth element:

{30,11,71}
ihatecsv
  • 532
  • 6
  • 20
1

Do not use as limits fixed values (such as 4), but instead use the length provided by the array(s).

for (int i = 0; i < m.length; i++) {
    for (int j = 0; j < m[i].length; j++) {
      //...
    }
}

Why?

Not all inner arrays have 4 elements (i.e.: {30, 11, 71}), so at the last iteration of the inner loop (j = 3), this code m[i][j] tries to access a value out of the bounds of the array because in some cases there is no position 3, just 0 (1st element), 1(2nd element) and 2(3rd element). Thus you get the mentioned exception.

Side note:

Another problem (mentioned by you) is that you will get r = 0 always because it is initialized to 0 and every time you multiply its value by another one, the result will be 0.

So, in order to fix this you need to add a check in the else condition, like this:

else {
    r = r == 0 ? m[i][j] : r * m[i][j];
}
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • @JohnDoe it's great you got it working now! Consider to mark an answer as [accepted](http://stackoverflow.com/help/someone-answers) so future readers know the problem was solved. – lealceldeiro Jun 19 '19 at 13:44