Hector
Hector

Reputation: 7025

Creating a chess board with system.out.println() in java

I am trying to create a chessboard that looks like this:

---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------

For some reason I cant get the last line on each row to appear where it suppose to.

Here is my code

   public static final int BOARD_SIZE = 8;

   public void displayChessBoard(){
      for (int row = 0; row < BOARD_SIZE; row++)
      {
          System.out.println("");
          System.out.println("---------------------------------");

          for (int column = 0; column < BOARD_SIZE; column++)
          {
              System.out.print("| " + " " + " ");
          }           
    }
    System.out.println("");
    System.out.println("---------------------------------");
  }

main just calls the method displayChessBoard().

Here is my output

---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------
|   |   |   |   |   |   |   |   
---------------------------------

Upvotes: 0

Views: 7772

Answers (3)

F2K
F2K

Reputation: 481

You are printing 7 pipes, inner for cycle is going from 0 to 7 (column < BOARD_SIZE), just add a pipe print after inner cycle's end.

If you don't mind extra spaces at the end of each line just change for condition to column <= BOARD_SIZE.

Upvotes: 1

i23
i23

Reputation: 496

You should change the BOARD_SIZE in second for-statement to 9:

This code generates the chess board just like you want:

public static void main(String args[])
{
    for (int row = 0; row < 8; row++)
    {
        System.out.println("");
        System.out.println("---------------------------------");

        for (int column = 0; column < 9; column++)
        {
            System.out.print("| " + " " + " ");
        }
    }
    System.out.println("");
    System.out.println("---------------------------------");
}

Upvotes: 0

Laerte
Laerte

Reputation: 7083

Add another print after the inner for. Like this:

for (int row = 0; row < BOARD_SIZE; row++)
  {
      System.out.println("");
      System.out.println("---------------------------------");

      for (int column = 0; column < BOARD_SIZE; column++)
      {
          System.out.print("| " + " " + " ");
      }       
      System.out.print("|");
    }
    System.out.println("");
    System.out.println("---------------------------------");

Hope it helps...

Upvotes: 4

Related Questions