I need to display Matrix tables using toString(). My code only displays the value in the last column of the last row. I cant work my way through it. It needs to come out like this:
1 2 3
4 5 6
7 8 9
It must have spaces after each digit and "\n" after each row, and has to be in a String toString() method. This is what I have so far:
1 2 3
4 5 6
7 8 9
It must have spaces after each digit and "\n" after each row, and has to be in a String toString() method. This is what I have so far:
public class Matrix {
int numRows;
int numColumns;
int data[][];
@Override // instruct the compiler that we do indeed intend for this method to override the superclass' (Object) version
public String toString()
{
String output = null;
/*
* TODO: replace the below return statement with the correct code, you must return a String that represents this
* matrix, as specified on the assignment page
*/
for (int i=0; i<numRows; i++)
for (int j=0; j<numColumns; j++)
output = data[i][j] + " ";
output +="\n";
return output; // placeholder
}
}