Skip to main content
deleted 29 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
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'sstatics. 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)

public class Hangman2 {

  • HangMan2 is a bad name, dont add numerals as class names. Add final modifier to class if you dont intend to override it.

static int guessesRemaining; static String stringWord; static char[] charWord;

You can write the code in purely OO manner, thus you dont need static's. Additionally, you may add 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("*")) {`

Cant 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)

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 statics. 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).

Source Link

public class Hangman2 {

  • HangMan2 is a bad name, dont add numerals as class names. Add final modifier to class if you dont intend to override it.

static int guessesRemaining; static String stringWord; static char[] charWord;

You can write the code in purely OO manner, thus you dont need static's. Additionally, you may add 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("*")) {`

Cant 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)