Write the method:
public int sumRow(int[][] matrix, int row)
that sums row row in the 2D array called matrix.
Given:
there is a problem with your second for loop's condition , matrix.length is the length of first dimension , for the second dimension it looks like matrix[i].length
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[i].length; j++){
sum = sum + matrix[i][j];
}
}
i prefer to use sum+=matrix[i][j] instead of sum = sum + matrix[i][j]
calculating for one row :
for(int j = 0; j < matrix[row].length; j++){
sum = sum + matrix[row][j];
}
just note that row's range is from 0 to matrix.length-1