0

I am trying to print out a gameBoard that has a "-" for each spot of the array: however every time I run this code I get this printed to the console:

[[C@2a139a55.

Any suggestions?

public class Game {

    public static void main(String[] args){

        char realBoard[][] = new char[7][7];

        for (int i=0;i<7;i++){
            for(int j=0;j<7;j++){
                realBoard[i][j]='-';
            }
        }
        System.out.print((realBoard));
    }
}
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
Msarn21
  • 11
  • 1
  • 6
  • `System.out.println(Arrays.deepToString(realBoard));` also, `for (int i = 0; i < realBoard.length; i++) { Arrays.fill(realBoard[i], '-'); }` – Elliott Frisch Nov 11 '18 at 23:21

3 Answers3

2

realBoard is an array, an object, so you can't just print it like that. You will need to iterate over the elements again

for(char[] y: realBoard) {
    for(char x: realBoard) {
        System.out.print(x);
    }
    System.out.println();
}
MegaBluejay
  • 426
  • 2
  • 8
0

Unless you need to use the array data of mark elsewhere, you would be better off just using print statements inside your loops.

for(int i = 0; i < 7; i++) {
  for(int j = 0; j < 7; j++) {
    //Print for each row
    System.out.print("-");
  }
  //Move to next line
  System.out.print("\n");
}
Tim Hunter
  • 242
  • 1
  • 7
0

You can't print a 2D array like that. To print a 2D array in one line you can use:

System.out.println(Arrays.deepToString(realBoard));

Or in multiple lines:

for(char[] x: realBoard)
            System.out.println(Arrays.toString(x));

Credits: Java - Best way to print 2D array?

ToTheMax
  • 981
  • 8
  • 18