public class Hangman2 {
HangMan2
is a bad name. Don't add numerals as class names. Add a 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 don't need static
s. Additionally, you may add a private
modifier.
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 can be declared as a string constant. You may not want infinite trial attempts.
stringWord = stringWord.toLowerCase();
Why not call it only word
?
while (livesRemaining > 0) {
Clearly warrants a method of its own.
while (b.matches(".*[^a-z].*")) {
Comes back to my point of a constant. Have you made sure it does not lead to NPE if string
is null
?
String a = new Hangman2().print(guessLetter);
A print
method is expected to be void
, else change the name. a
is a bad name.
if (!a.contains("*")) {
Can't this be replaced with letterRemaining == 0
?
System.out.print(misses + " ");
Assume you could print a string at a time?
char[] a = s.toCharArray();
You could opt for String.charAt(i)
.