• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java hangman problem  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in the processes of constructing a hangman game in java where one player types in the word and the other has to guess letters. The current problem I am facing with is when I type in a character, it outputs the list of characters in the array. I am trying where it only prints out one character depending on the user. Thanks

 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't manage to get all your code into the main method, so you might as well move it out now. Consider what sort of classes you will need.
You are printing the char[] in line 22. You will have to decide what to print and then print it. I woiuld have thought you will have to ask for a character: there is no simple method of reading a single char from system.in. The nearest is probably to read an ordinary String and take its first character. If you ask for a letter it will be entered as a one‑letter String.
Why are you using a buffered reader for System.in when a Scanner is easier, and doesn't throw any exceptions?

Your formatting could do with a bit of improvement. Line 19 is too long. It shou‍ld be divided into several lines.
Don't put spaces before ( as in line 11 or after { in line 19.
Put single empty lines before and after the import line. There are too many empty lines in the method.
It would be better not to have * in imports.
The // comments are useless since they don't tell us anything we don't know already. You may have had the misfortune of being told to use such comments.
Don't declare local variables (line 8) until you are ready to use them. You can declare the String on the same line as it is initialised (line 19). At least in the code you have shown you can.
 
salvin francis
Bartender
Posts: 1455
19
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from what campbell said, "hangman" is not good name for a class. You may want to look at java class naming conventions for a better name.
Another practical aspect is "one player types in the word and the other has to guess letters". How are you planning to achieve that through a console based program ?
If you want to hide text in a "password" like field, you might want to consider UI coding (such as javaFx or swing)
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For simpler way to get hidden text from a user, look at using a JPasswordField in a JOptionPane.showMessageDialog
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or the Console class, but you can't use that for a non‑console application.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!