I'm trying to get the sum of each row printed out after that row. It needs to be formatted like this
2 5 6 3| 16
9 4 4 7| 24
1 10 2 3| 16
8 4 5 3| 20
-----------------
20 23 17 16| 76
Doesn't have to be as nice, but close to it. I have to use a toString method, here is what I have so far.
Edit: Got it solved thanks to @KevinAnderson. Needed to append rowSum[r]
to Arrays instead of trying to use another for loop to get the values.
public String toString() {
//Stores the colSum into a string col
String col = "";
for (int j = 0; j < cell[0].length; j++) {
col += " " + colSum[j];
}
//Calculates the total of both the rows and columns
for (int i = 0; i < cell.length; i++) {
for (int j = 0; j < cell[0].length; j++) {
grandTotal += cell[i][j];
}
}
//Prints the array
String array = "";
for (int r = 0; r < cell.length; r++) {
for (int c = 0; c < cell[r].length; c++) {
array += " " + cell[r][c];
}
array += "|" + +rowSum[r] + "\n";
}
return array + "-----------------" + "\n" + col + "| " + grandTotal;
}
rowSum
and colSum
are calculated in a separate method that can be provided if need be, but they do work as intended.
What I believe I need to do is store the value of rowSum
in either an int value and print it, or increment through rowSum
in some way. I've tried both, and so far it prints out the whole array that it is stored into.