954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with 2d array

ok so I need to use a 2d array to draw a hangman picture for a hangman game. the picture should be something like this
============
||
|O
|/|\
|/\
|
|
but when i run this code with main. i get this

=
=
=
=
=
=
||
null
null
|
null
null
||
null
null
O
null
null
||
null
/
|
null
null
||
null
null
null
null
null
||
null
null
null
null
null

private void drawHangman(){

    int rows = 6;
    int cols = 6;
    String[][] draw = new String[rows][cols];

    for(int n=1; n<rows; n++){//draw hang pole ||
        draw[n][0] = "||";
    }

    for(int r=0; r<cols; r++){//draw hang pole =
        draw[0][r]= "=";
    }
    draw[1][3] = "|";

    if (wrong >= 1){//draw head
        draw[2][3] = "O";
    }
    if (wrong >= 2){//draw body
        draw[3][3] = "|";
    }
    if (wrong >= 3){//draw hand
        draw[3][2] = "/";
    }
    if (wrong >= 4){//draw hand
        draw[3][4] = "\\";
    }
    if (wrong >= 5){//draw leg
        draw[4][2] = "/";
    }
    if (wrong == 6){//draw leg
        draw[4][4] = "\\";
    }
    
    for(int x = 0; x < rows; x++){
        for(int y = 0;y < cols; y++){
            System.out.println(draw[x][y]);
        }
        
    }
    
}
number87
Junior Poster in Training
83 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

System.out.println (short for Print Line) puts a newline char at the end whatever it prints, so you get one array element on each line.
System.out.print does the same as System.out.println but does not add the newline, so that's the one you need. You'll then need to do your own newline "\n" at the end of each row.
To get rid of the nulls, you need to ensure that every element contains a valid String (eg " ");

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You