Kamon241
Kamon241

Reputation: 75

Trying to print a 2D array of Chars in Java

My program doesn't print anything at all.

First I initialized the board in a seperate class (board):

       public class Board {
          public char board[][] = new char[9][9];   
          public void main(char[] args){

        for(int i=0; i<9; i++){
            board[i][0] = '_';
            board[i][8] = '_';
        }
        for(int h=0; h<9; h++){
            board[0][h] = '|';
            board[8][h] = '|';
        }

        for(int x=0; x>9; x++){
            for(int y=0; y>9; y++){
                System.out.println(board[x][y]);    
            }
        }
    }
}

Then called it in the main, with a PrintLine of "Hello World" to check that the code was being accessed. No errors are flagged up but neither does it print anything at all. The Main is below also just to check that i havent done anything simple and stupid:

    public static void main(String[] args) {  
    Ticker T = new Ticker();
    Board B = new Board();       
    for(int x=0; x>9; x++){
        for(int y=0; y>9; y++){
            System.out.println("Hello World");
            System.out.print(B.board[x][y]);

Upvotes: 3

Views: 12574

Answers (5)

zapl
zapl

Reputation: 63955

Besides the incorrect condition in the for loops you should consider using

public class Board {
    public char board[][] = new char[9][9];

    // this is the constructor, it will be called if you say "new Board()"
    // the "main" method you had here will not be called automatically
    public Board() {
        for (int i = 0; i < 9; i++) {
            board[i][0] = '_';
            board[i][8] = '_';
        }
        for (int h = 0; h < 9; h++) {
            board[0][h] = '|';
            board[8][h] = '|';
        }

        for (int x = 0; x < 9; x++) {
            for (int y = 0; y < 9; y++) {
                // just a print so it does not make new lines for every char
                System.out.print(board[x][y]);
            }
            // new line once one column (board[x][0] - board[x][8]) is printed
            // note: you proably want to turn around the x and y above since
            // I guess you want to print rows instead of columns
            System.out.println();
        }
    }
}

It fixes some problems

  • the condition of the for loop that could never be true
  • replaced the main method with a constructor so the code you wrote there is executed
  • changed to print the stuff that is printed in the inner loop in one line, so it looks like a board

Now if you do

public static void main(String[] args) {  
    Ticker T = new Ticker();
    Board B = new Board(); // << this line triggers printing
    // ...
}

you should see some board like thing

Upvotes: 1

semTex
semTex

Reputation: 343

have a look at for(int x=0; x>9; x++){

It should be for(int x=0; x<9; x++){

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

There is problem in your condition of for loop: -

for(int x=0; x>9; x++){
        for(int y=0; y>9; y++){

The code inside above loop never gets executed. It should be: -

for(int x=0; x<9; x++){
        for(int y=0; y<9; y++){

Upvotes: 1

autra
autra

Reputation: 962

You might want to go for x<9 and y<9 and not > ! ;-) This is the condition loop. If it is false, the loop exit. It is always false in your case.

Upvotes: 0

hmjd
hmjd

Reputation: 121961

The terminating conditions on the for loops are incorrect. Should be <, not >. Change to:

for(int x=0; x<9; x++){
    for(int y=0; y<9; y++){

Upvotes: 2

Related Questions