Ask your own question, for FREE!
Computer Science 30 Online
OpenStudy (anonymous):

in java I am getting an out of bounds exception. working with 2-dim arrays. for(int r=0;r getRowTotal(a, (r+1))) { max=getRowTotal(a, r); rowNumber=r; } else max=getRowTotal(a, (r+1)); }

OpenStudy (anonymous):

So, perhaps you should do a System.out.println(a.length); Which will show you the length of a. Also note that while your loop specifies r<a.length which is the common method of going through the array, your are checking r+1 which would be beyond the length of the array once you get to the last row.

OpenStudy (anonymous):

here is what i switched it to: for(int r=0;r<a.length;r++){ if (getRowTotal(a, r) > max) { max=getRowTotal(a, r); rowNumber=r; } }

OpenStudy (anonymous):

getRowTotal method looks like this: public static int getRowTotal(int[][] a, int row){ int total = 0; for (int i = 0; i < a[row].length; i++) total += a[row][i]; return total; }

OpenStudy (anonymous):

Sorry, not sure if the problem was solved or not. Is it working now? Please note that your running getRowTotal twice. I would assign a variable to get the value and then if it's greater assign that to max rather than having to find the total multiple times. Just makes it a bit more efficient.

OpenStudy (anonymous):

the array a doesn't have a (r+1)th element when r=a.length-1.

OpenStudy (anonymous):

He changed it. It used to, then I commented and he changed it to the current version.

OpenStudy (anonymous):

oh, ok my bad. broinjc probably followed up to post the final solution. i'd consider this one solved.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!