string guessing game in java import java.util.Scanner;

class Main {

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

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

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

            for(int i = 0; i &lt; 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(&quot;-&quot;, guess);
                        temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
                    }
                } else {
                    if(!iterated) {
                        temp += &quot;-&quot;;
                    }
                }
            }
            tries++;
            iterated = true;
            System.out.println(temp);
            if(temp.equals(word)) {
                System.out.println(&quot;You guessed correctly!&quot;);
                break;
            }
        }while (tries &lt; 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 &quot;sorry you didn't guess it. the word was (sleepy)&quot;. Thank you!
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 &quot;sleepy&quot;.

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 = &quot;&quot;;
        System.out.println(&quot;Welcome to the guessing game! Guess one letter &quot;
                + &quot;at a time. You have &quot; + incorrectGuesses + &quot; incorrect &quot;
                        + &quot;guesses remaining.&quot;);

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

            for (int i = 0; i &lt; 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(&quot;-&quot;, guess);
                        temp = temp.substring(0, i) + holder + 
                                temp.substring(i + 1, temp.length());
                    }
                } else {
                    if (!iterated) {
                        temp += &quot;-&quot;;
                    }
                }
            }
            iterated = true;
            
            if (correctGuess) {
                System.out.println(&quot;The word contains the letter &quot; + guess + &quot;.&quot;);
            } else {
                System.out.println(&quot;The word does not contain the letter &quot; + guess + &quot;.&quot;);
                incorrectGuesses--;
                System.out.println(&quot;You have &quot; + incorrectGuesses + 
                        &quot; incorrect guesses remaining.&quot;);
            }
            
            System.out.println(temp);
            if (temp.equals(word)) {
                System.out.println(&quot;You guessed correctly!&quot;);
                break;
            }
        
        } while (incorrectGuesses &gt; 0);
        
        if (incorrectGuesses &lt;= 0) {
            System.out.println(&quot;Sorry, you didn't guess the secret word \&quot;&quot; + word + &quot;\&quot;.&quot;);
        }
        
        reader.close();

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

}