The program below (thanks to Sundial) computes the area of a rectangle
public class ComputeTheArea {
public static int areaOfTheRectangle (char[][] table, char ch) {
int[] first = new int[2];
int[] last = new int[2];
for (int i=0; i<3; i++) {
for (int j=0; j<4; j++) {
if(grid[i][j]==ch) {
first[0] = i;
first[1] = j;
}
}
}
for (int i=2; i>=0; i--) {
for (int j=3; j>=0; j--) {
if(grid[i][j]==ch) {
last[0] = i;
last[1] = j;
}
}
}
int answer = ((Math.max(first[0]+1,last[0]+1) - Math.min(first[0]+1,last[0]+1)) *
(Math.max(first[1]+1,last[1]+1) - Math.min(first[1]+1,last[1]+1)));
return answer;
}
However, when it is run, it outputs the wrong answer. I know there is something wrong with the for loop. I'm new in Java and I need your help for me to fix the method. Please and thank you very much!
EDIT: I edited the code to conform to Michael's answer.