2
\$\begingroup\$
import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int tries = 0;
        boolean iterated = false;
        String temp = "";
        String holder = "";
        System.out.println("Welcome to the guessing game! Guess one letter at a time. You have 5 incorrect guesses remaining.");

        Scanner reader = new Scanner(System.in);
        System.out.println("The Secret Word Is: ______ (6)");
        String word = "sleepy";

        do {
            System.out.print("Your Guess: ");
            String guess = reader.nextLine();

            for(int i = 0; i < word.length(); i ++) {
                if (guess.equals(Character.toString(word.charAt(i)))) {
                    if(!iterated)
                        temp += Character.toString(word.charAt(i));
                    else {
                        holder = Character.toString(temp.charAt(i)).replace("-", guess);
                        temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
                    }
                } else {
                    if(!iterated) {
                        temp += "-";
                    }
                }
            }
            tries++;
            iterated = true;
            System.out.println(temp);
            if(temp.equals(word)) {
                System.out.println("You guessed correctly!");
                break;
            }
        }while (tries < 5);

    }
}

So this is the code I have so far. It works pretty well but I was wondering how I could make it so that every time you add enter a letter, it tells you whether it was right or wrong and how many tries you have remaining. Also I was wondering how i could make it so that if the user doesn't guess the word I can say something like "sorry you didn't guess it. the word was (sleepy)". Thank you!

\$\endgroup\$
1
  • \$\begingroup\$ Welcome To Code Review, I formatted your java code and added the hangman tag to your post. \$\endgroup\$ – dariosicily Apr 12 at 21:03
2
\$\begingroup\$

I reworked your code a little. Here are the results from one test run.

Welcome to the guessing game! Guess one letter at a time. You have 5 incorrect guesses remaining.
The Secret Word Is: ------ (6)
Your Guess: a
The word does not contain the letter a.
You have 4 incorrect guesses remaining.
------
Your Guess: s
The word contains the letter s.
s-----
Your Guess: d
The word does not contain the letter d.
You have 3 incorrect guesses remaining.
s-----
Your Guess: f
The word does not contain the letter f.
You have 2 incorrect guesses remaining.
s-----
Your Guess: p
The word contains the letter p.
s---p-
Your Guess: g
The word does not contain the letter g.
You have 1 incorrect guesses remaining.
s---p-
Your Guess: h
The word does not contain the letter h.
You have 0 incorrect guesses remaining.
s---p-
Sorry, you didn't guess the secret word "sleepy".

Instead of counting tries up from zero, I counted down from 5. If you decide to change the number of incorrect guesses, you only have to change it in one place.

I wrote a method to create a dashed word from the word for the secret word message. I was hoping I could use the dashed word later, but I couldn't.

I still don't understand the character substitution for loop, but it works, so I left it alone. Using a StringBuilder would greatly simplify your character substitution.

Here's the modified code.

import java.util.Scanner;

public class StringGuessingGame {

    public static void main(String[] args) {
        int incorrectGuesses = 5;
        boolean iterated = false;
        String temp = "";
        System.out.println("Welcome to the guessing game! Guess one letter "
                + "at a time. You have " + incorrectGuesses + " incorrect "
                        + "guesses remaining.");

        Scanner reader = new Scanner(System.in);
        
        String word = "sleepy";
        String dashedWord = createDashedWord(word);
        System.out.println("The Secret Word Is: " + dashedWord + 
                " (" + word.length() + ")");
        
        do {
            System.out.print("Your Guess: ");
            String guess = reader.nextLine();
            boolean correctGuess = false;

            for (int i = 0; i < word.length(); i++) {
                if (guess.equals(Character.toString(word.charAt(i)))) {
                    correctGuess = true;
                    if (!iterated)
                        temp += Character.toString(word.charAt(i));
                    else {
                        String holder = Character.toString(
                                temp.charAt(i)).replace("-", guess);
                        temp = temp.substring(0, i) + holder + 
                                temp.substring(i + 1, temp.length());
                    }
                } else {
                    if (!iterated) {
                        temp += "-";
                    }
                }
            }
            iterated = true;
            
            if (correctGuess) {
                System.out.println("The word contains the letter " + guess + ".");
            } else {
                System.out.println("The word does not contain the letter " + guess + ".");
                incorrectGuesses--;
                System.out.println("You have " + incorrectGuesses + 
                        " incorrect guesses remaining.");
            }
            
            System.out.println(temp);
            if (temp.equals(word)) {
                System.out.println("You guessed correctly!");
                break;
            }
        
        } while (incorrectGuesses > 0);
        
        if (incorrectGuesses <= 0) {
            System.out.println("Sorry, you didn't guess the secret word \"" + word + "\".");
        }
        
        reader.close();

    }
    
    private static String createDashedWord(String word) {
        String output = "";
        for (int i = 0; i < word.length(); i++) {
            output += "-";
        }
        return output;
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.