A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
in java I am getting an out of bounds exception. working with 2dim arrays. for(int r=0;r<a.length;r++){
if (getRowTotal(a, r) > getRowTotal(a, (r+1)))
{
max=getRowTotal(a, r);
rowNumber=r;
}
else max=getRowTotal(a, (r+1));
}
anonymous
 5 years ago
in java I am getting an out of bounds exception. working with 2dim arrays. for(int r=0;r<a.length;r++){ if (getRowTotal(a, r) > getRowTotal(a, (r+1))) { max=getRowTotal(a, r); rowNumber=r; } else max=getRowTotal(a, (r+1)); }

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0So, 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0here 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; } }

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0getRowTotal 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; }

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Sorry, 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0the array a doesn't have a (r+1)th element when r=a.length1.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0He changed it. It used to, then I commented and he changed it to the current version.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0oh, ok my bad. broinjc probably followed up to post the final solution. i'd consider this one solved.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.