fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3.  
  4. import java.util.stream.Collectors;
  5. import java.util.stream.IntStream;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9.  
  10. public static final int WIDTH = 5;
  11. public static final int HEIGHT = 10;
  12.  
  13. public static void main(String[] args) {
  14. String[][] generateGrid = generateGrid(WIDTH, HEIGHT);
  15. printGrid(WIDTH,HEIGHT, generateGrid);
  16. }
  17.  
  18. public static String[][] generateGrid(int width, int height) {
  19. String[][] gridData = new String[height][width];
  20. for (int row = 0; row < height; row++) {
  21. for (int column = 0; column < width; column++) {
  22. gridData[row][column] = " ";
  23. }
  24. }
  25. return gridData;
  26. }
  27.  
  28. public static String padRight(String s, int n) {
  29. return String.format("%-" + n + "s", s);
  30. }
  31.  
  32. public static String padLeft(String s, int n) {
  33. return String.format("%" + n + "s", s);
  34. }
  35.  
  36. public static String repeatChar(char c, int n) {
  37. return IntStream.range(0, n).mapToObj(i -> String.valueOf(c)).collect(Collectors.joining(""));
  38. }
  39.  
  40. public static void printGrid(int width, int height, String[][] gridData) {
  41. System.out.printf("%d x %d Grid%n", width, height);
  42. int lengthOfMaxRowNum = String.valueOf(height-1).length();
  43. // TODO: can be calculated as max length over Strings in gridData
  44. int maxCellWidth = 4;
  45.  
  46. System.out.print(padRight(" ", lengthOfMaxRowNum));
  47. for (int column = 0; column < width; column++) {
  48. System.out.print(padLeft(String.valueOf(column), maxCellWidth + 1));
  49. }
  50. System.out.println();
  51. printHorizontalLine(width, lengthOfMaxRowNum, maxCellWidth);
  52. System.out.println();
  53.  
  54. for (int row = 0; row < height; row++) {
  55. // TODO: alignment of headers (col-numbers) could be customizable
  56. System.out.print(padLeft(String.valueOf(row), lengthOfMaxRowNum));
  57. for (int column = 0; column < width; column++) {
  58. // TODO: alignment of cell-data could be customizable
  59. System.out.print("|" + padLeft(gridData[row][column], maxCellWidth));
  60. }
  61. System.out.println("|");
  62. printHorizontalLine(width, lengthOfMaxRowNum, maxCellWidth);
  63. System.out.println();
  64. }
  65. }
  66.  
  67. private static void printHorizontalLine(int width, int lengthOfMaxRowNum, int maxCellWidth) {
  68. String line = repeatChar('-', maxCellWidth);
  69. System.out.print(padLeft(" ", lengthOfMaxRowNum));
  70. for (int column = 0; column < width; column++) {
  71. System.out.printf("+"+line);
  72. }
  73. System.out.printf("+");
  74. }
  75.  
  76.  
  77. }
Success #stdin #stdout 0.15s 2184192KB
stdin
Standard input is empty
stdout
5 x 10 Grid
     0    1    2    3    4
 +----+----+----+----+----+
0|    |    |    |    |    |
 +----+----+----+----+----+
1|    |    |    |    |    |
 +----+----+----+----+----+
2|    |    |    |    |    |
 +----+----+----+----+----+
3|    |    |    |    |    |
 +----+----+----+----+----+
4|    |    |    |    |    |
 +----+----+----+----+----+
5|    |    |    |    |    |
 +----+----+----+----+----+
6|    |    |    |    |    |
 +----+----+----+----+----+
7|    |    |    |    |    |
 +----+----+----+----+----+
8|    |    |    |    |    |
 +----+----+----+----+----+
9|    |    |    |    |    |
 +----+----+----+----+----+