943,729 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 522
  • Java RSS
Apr 18th, 2009
0

help with 2d array

Expand Post »
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
============
|<space>|
|<space>O
|<space>/|\
|<space>/\
|
|
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

Java Syntax (Toggle Plain Text)
  1. private void drawHangman(){
  2.  
  3. int rows = 6;
  4. int cols = 6;
  5. String[][] draw = new String[rows][cols];
  6.  
  7. for(int n=1; n<rows; n++){//draw hang pole ||
  8. draw[n][0] = "||";
  9. }
  10.  
  11. for(int r=0; r<cols; r++){//draw hang pole =
  12. draw[0][r]= "=";
  13. }
  14. draw[1][3] = "|";
  15.  
  16. if (wrong >= 1){//draw head
  17. draw[2][3] = "O";
  18. }
  19. if (wrong >= 2){//draw body
  20. draw[3][3] = "|";
  21. }
  22. if (wrong >= 3){//draw hand
  23. draw[3][2] = "/";
  24. }
  25. if (wrong >= 4){//draw hand
  26. draw[3][4] = "\\";
  27. }
  28. if (wrong >= 5){//draw leg
  29. draw[4][2] = "/";
  30. }
  31. if (wrong == 6){//draw leg
  32. draw[4][4] = "\\";
  33. }
  34.  
  35. for(int x = 0; x < rows; x++){
  36. for(int y = 0;y < cols; y++){
  37. System.out.println(draw[x][y]);
  38. }
  39.  
  40. }
  41.  
  42. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Apr 18th, 2009
0

Re: help with 2d array

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 " ");
Last edited by JamesCherrill; Apr 18th, 2009 at 10:13 am.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Database problem
Next Thread in Java Forum Timeline: Null pointer Exception





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC