Your algorithm is a good point to start. Anyway I would like to broaden your perspective on extended use-cases and the usage of existing (reliable and customizable) text-based tabular-formatting libraries.
Use this extended solution and optimize
It is based on your approach. I added following features:
- customizable grid-size (width, height as parameters)
- utility functions: padRight, padLeft, repeatChar
You can further optimize that, for example:
- left or right alignment of headers/data
- calculation of the maximum cell-space need (max length of grid-data)
Source (minimal Java 8 streaming used)
Find source below or online where you can test & download on Ideone.
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class GridDataToTextTable {
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
public static void main(String[] args) {
System.out.printf("%d x %d Grid%n", WIDTH, HEIGHT);
String[][] generateGrid = generateGrid(WIDTH, HEIGHT);
printGrid(WIDTH, HEIGHT, generateGrid);
}
public static String[][] generateGrid(int width, int height) {
String[][] gridData = new String[height][width];
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
gridData[row][column] = " ";
}
}
return gridData;
}
public static String padRight(String s, int n) {
return String.format("%-" + n + "s", s);
}
public static String padLeft(String s, int n) {
return String.format("%" + n + "s", s);
}
public static String repeatChar(char c, int n) {
return IntStream.range(0, n).mapToObj(i -> String.valueOf(c)).collect(Collectors.joining(""));
}
public static void printGrid(int width, int height, String[][] gridData) {
int lengthOfMaxRowNum = String.valueOf(height - 1).length();
// TODO: can be calculated as max length over Strings in gridData
int maxCellWidth = 4;
System.out.print(padRight(" ", lengthOfMaxRowNum));
for (int column = 0; column < width; column++) {
System.out.print(padLeft(String.valueOf(column), maxCellWidth + 1));
}
System.out.println();
printHorizontalLine(width, lengthOfMaxRowNum, maxCellWidth);
System.out.println();
for (int row = 0; row < height; row++) {
// TODO: alignment of headers (col-numbers) could be customizable
System.out.print(padLeft(String.valueOf(row), lengthOfMaxRowNum));
for (int column = 0; column < width; column++) {
// TODO: alignment of cell-data could be customizable
System.out.print("|" + padLeft(gridData[row][column], maxCellWidth));
}
System.out.println("|");
printHorizontalLine(width, lengthOfMaxRowNum, maxCellWidth);
System.out.println();
}
}
private static void printHorizontalLine(int width, int lengthOfMaxRowNum, int maxCellWidth) {
String line = repeatChar('-', maxCellWidth);
System.out.print(padLeft(" ", lengthOfMaxRowNum));
for (int column = 0; column < width; column++) {
System.out.printf("+" + line);
}
System.out.printf("+");
}
}
Use Text-Formatting libraries
To print out data text-based in tabular or grid format see this answer.
Maybe you can use one of following libraries: