I am looking to format the output of this code so that the totals of the rows and columns show up next to/below said row/column. I am getting the numbers correct, but spaces and char counts seem to be too unpredictable to format it correctly for any size square. Here is the code i currently have.
public void printSquare()
{
for (int x = 0; x < square.length; x ++)
{
System.out.println();
for (int j = 0; j < square.length; j++)
{
System.out.print(" " + square[x][j]);
}
System.out.println(" | " + sumRow(x));
}
System.out.print(sumOtherDiag());
for (int x = 0; x < square.length; x ++)
{
System.out.print(" ^");
}
System.out.println(sumMainDiag());
for (int x = 0; x < square.length; x ++)
{
System.out.print(" " + sumCol(x));
}
}
And the output for this is..
8 1 6 | 15
3 5 7 | 15
4 9 2 | 15
15 ^ ^ ^15
15 15 15
Are there any strategies in order to more reliably format something like this?