public class Hangman2 {
public class Hangman2 {HangMan2
- HangMan2 is a bad name, dont add numerals as class names. Add final modifier to class if you dont intend to override it.
is a bad name. Don't add numerals as class names. Add a static int guessesRemaining; static String stringWord; static char[] charWord;final
modifier to the class if you don't intend to override it.
static int guessesRemaining;
static String stringWord;
static char[] charWord;
You can write the code in purely OO manner, thus you dontdon't need static'sstatic
s. Additionally, you may add privatea private
modifier.
public static void playHangman() {
No need for public static void playHangman() {
No need for static.
`//Loop to get user to enter in letters only
while (stringWord.matches(".*[^a-z].*")) {
System.out.println("Please enter letters only, try again:");
stringWord = scan.nextLine();
}`
This can be a different method. Regex Regex can be declared as a string constant. You You may not want infinite trial attempts.
`stringWordstringWord = stringWord.toLowerCase();`;
Why not call it only word word
?
`whilewhile (livesRemaining > 0) {`
Clearly warrants a method of its own.
`whilewhile (b.matches(".*[^a-z].*")) {`
Comes back to my point of a constant.
Have Have you made sure it does not lead to NPE if stringstring
is null null
?
`StringString a = new Hangman2().print(guessLetter);`;
A printprint
method is expected to be voidvoid
, else change the name.
'a' a
is a bad name.
`ifif (!a.contains("*")) {`
CantCan't this be replaced with letterRemaining == 0letterRemaining == 0
?
`SystemSystem.out.print(misses + " ");`;
Assume you could print a string at a time ?
`char[]char[] a = s.toCharArray();`;
You could opt for StringString.charAt(i)
.charAt(i)