TomateFraiche
TomateFraiche

Reputation: 134

Hangman in JAVA FX displays wrong letters

I got a question on my Hangman in JAVAFX. I need to randomly choose and then guess letter by letter one word from my predefined array of words.

The problem is that the code, which has to replace the dashes with guessed letters, works very strange (wrong) with the first key stroke.

In essence, whatever first letter you guess, by some unknown reason the program always pops up a different, its own favorite letter or letters (it can be 2 or even 3 letters shown after the first key stroke). The guessing goes right from second letter on, but the problem with first key stroke happens all the time and I would need to understand why?

I feel the problem could be connected to my Java Random class because everything works fine when I do not use it, I mean when I set up a one secret word beforehand and then replace its dashes with the correctly-guessed letters, then it works well.

Examples of my codes with and without Random follow here:

  1. With random and array. Works wrong:

    public class Main extends Application {
        String wordToGuess;
        String[] allTheWords = {
            "light", "yawning", "sleeping"
        };
        String g;
        String lettersGuessed;
        Random random = new Random();
        Label label1 = new Label();
        TextField tField1 = new TextField();@Override
        public void start(Stage primaryStage) {
            wordToGuess = allTheWords[random.nextInt(allTheWords.length)];
            tField1.setOnKeyReleased(e -> {
                g = tField1.getText().toLowerCase();
                tField1.setText("");
                lettersGuessed += g;
    
    
                label1.setText(fillTheWord());
            });
            VBox root = new VBox();
            root.setAlignment(Pos.CENTER);
            root.getChildren().addAll(label1, tField1);
            Scene scene = new Scene(root, 400, 400);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public String fillTheWord() {
            String randomValue = "";
            for (char l: wordToGuess.toCharArray()) {
                if (lettersGuessed.contains(Character.toString(l))) {
                    randomValue += l + "";
                } else {
                    randomValue += "_ ";
                }
            };
            return randomValue;
        }
        public static void main(String[] args) {
            launch(args);
        }
    }
    
  2. Without random and array. Works OK:

    public class Main extends Application {
        TextField tField1 = new TextField();
        Label label1 = new Label("your word");
        String theWord = "vitamine";
        String lettersGuessed = "";
    
        public void start(Stage primaryStage) {
            TextField tField1 = new TextField();
            VBox vbox1 = new VBox(20);
            vbox1.getChildren().addAll(label1, tField1);
            tField1.setOnKeyReleased(e -> {
                String g = tField1.getText().toLowerCase();
                tField1.setText("");
                lettersGuessed += g;
                label1.setText(fillTheWord());
            });
            Scene scene1 = new Scene(vbox1, 400, 400);
            primaryStage.setScene(scene1);
            primaryStage.show();
        }
    
        public String fillTheWord() {
            String rValue = "";
            for (char l: theWord.toCharArray()) {
                if (lettersGuessed.contains(Character.toString(l))) {
                    rValue += l + "";
                } else {
                    rValue += "_ ";
                }
            }
            return rValue;
        }
        public static void main(String[] args) {
            launch(args);
        }
    }
    

Any assistance is extremely and exceptionally valuable.

PS: I would like to illustrate my question with screenshots but the lack of Stackoverflow reputation prevents me from doing so.

Upvotes: 0

Views: 995

Answers (2)

user4979686
user4979686

Reputation:

After loading both of your programs up and running both, this is what I found....

The lettersGuessed variable is not initialized to a value the first time it is run. So instead, it searches for all of the characters in the word "null", AND the character you typed; you can see this by changing the value of your "wordToGuess" to the alphabet as a string. If you do this, you'll notice the character you pressed, plus the letters n, u, and l are all selected as guessed. enter image description here

In my case, I hit G.

To fix this, you can simply initialize your lettersGuessed variable...

String lettersGuessed = "";

As a side note, I believe this is because the valueOf function is called on the empty string object. When that function is called, it returns a string of "null" if the object is null. In this case, it returns "null" + the character you typed, which is then checked in the fillTheWord function, filling in n, u, and l values.

Upvotes: 0

rlinden
rlinden

Reputation: 2041

All you need to do is initialize the lettersGuessed variable, with String lettersGuessed=""; You did it in the second version, so it worked just fine.

I hope it helps.

Upvotes: 0

Related Questions