So I wrote a program that is supposed to take a 2D array as an input and and calculate the sum of each row and each column. Then I need to use a toString()
method to print the result. For example for the intake:
{3,6,8,9},{5,4,3,2}
The output should look like this:
3 6 8 9 | 26
5 4 3 2 | 14
8 10 11 11 | 40
The problem is that I don't have a clue how to print each row in a separate line using the toString
method. Currently what I have is:
public String toString() {
String output="";
for (int i=0;i<resultArray.length;i++) {
for (int j=0;j<resultArray[i].length;j++) {
System.out.print(output+resultArray[i][j]+" ");
}
}
return output;
}
Which returns the following: 3 6 8 9 5 4 3 2
(Don't worry about the summation methods, I have those ready I just need to understand how to format it before i'll use them)
Anyone has an idea how to print each row in its own line?