Jason
Jason

Reputation: 23

Java 10 * 10 grid

First post here, thanks in advance for any help. I have made a 10*10 grid in Java and am trying to get the row numbers to appear on the left side of the grid, after many attempts at different print formats and options I am now here for a little help. Any pointers will be greatly appreciated.

public class ArrayTest {

public final static int SIZE =10;
final static char[][] GRID = new char[SIZE][SIZE];

public static void main(String[] args){

    setGrid();       
    printGrid();
    }

public static void setGrid(){
    for( int row = 0; row< SIZE;row++){
        for(int column = 0; column<SIZE; column++){
            GRID[row][column]= ' ';
        }
    }
}

public static void printGrid(){
    System.out.println("   10 x 10 Grid");
    System.out.println("    0   1   2   3   4   5   6   7   8   9");
    System.out.println("  +---+---+---+---+---+---+---+---+---+---+");


    for(int row = 0; row< SIZE; row++){
        for(int column = 0; column<SIZE; column++){
               System.out.print("  |" + GRID[row][column] + "");
        }


                System.out.println("  | " + row );
                System.out.println("  +---+---+---+---+---+---+---+---+---+---+");

    }
}

}

Upvotes: 2

Views: 944

Answers (2)

hc_dev
hc_dev

Reputation: 9418

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:

Upvotes: 0

FredK
FredK

Reputation: 4084

for(int row = 0; row< SIZE; row++){
   System.out.print( " " + row + " | ");
   for ( int column = 0; column<SIZE; column++){
     ... <print column values for this row here>
   }
   System.out.println("");
 }

Don't forget to add extra spaces when you print out the column numbers at the top, to account for the space used up by the row number indicators.

Upvotes: 1

Related Questions