I am assuming you know how to build the string on the left and on the right, using StringBuilder
or String concatenation.
Once you have your strings built, you can use printf
as below to pad the output on the right to as many whitespace characters as you desire. In the sample code below, I have used 20
as the number of characters to pad to.
public class Pad {
public static void main(String[] args) {
String a = "0.0 0.0 0.0 ";
String b = "0.0 0.0 0.0 0.0";
String c = "[ 0.0, 0.0]";
System.out.printf("%-20s %s\n", a, c);
System.out.printf("%-20s %s\n", b, c);
}
}
You can use this idea in your code above by replacing your last loop with:
// printing the array
for(int row=0; row<figures.length; ++row) {
// printing data row
StringBuilder sb = new StringBuilder();
group = (char)((row)+(int)'A');
sb.append(group+" : ");
for(int col=0; col<figures[row].length; ++col) {
sum += figures[row][col];
average = sum/figures[row].length;
x = " "+figures[row][col];
diff = minLen - x.length();
sb.append(String.format("%1$" + diff + "s", x));
sb.append(" ");
}
System.out.printf("%-75s[%f]\n", sb.toString(), average);
}