Guessing method (pretty basic java)
<p>I'm using the following code to add a guessed consonant to a string of stars if the guessed consonant is part of the original word. Initially I was keeping <code>wordWithGuess</code> between calls to <code>getCurrentResult</code>. But the result of this was that the new content was added to the end, and <code>wordWithGuess</code> kept getting longer (instead of just replacing the most recently guessed letter).</p>

<p>When running the code below, the output is</p>

<pre>
After guessing r: *****r******
After guessing s: ************
After guessing t: **tt********
After guessing l: ********ll**
After guessing n: ***********n
</pre>

<p>My goal is for it to be:</p>

<pre>
After guessing r: *****r******
After guessing s: *****r******
After guessing t: **tt*r******
After guessing l: **tt*r**ll**
After guessing n: **tt*r**ll*n
</pre>

<p>Sample code follows:</p>

<pre><code>public class Sample {
    String targetWord;
    String wordWithGuess = "";

    public Sample(String targetWord) {
        this.targetWord = targetWord;
    }

    public void guess(String consonant) {
        wordWithGuess = "";

        for (int i = 0; i &lt; targetWord.length(); i++) {
            if (targetWord.substring(i, i + 1).equals(" ")) {
                wordWithGuess += " ";
            } else if (targetWord.substring(i, i + 1).equals(consonant)) {
                wordWithGuess += consonant;
            } else {
                wordWithGuess += "*";
            }
        }
    }

    public String getCurrentResult() {
        return wordWithGuess;
    }

    public static void main(String args[]) {
        String targetWord = "bitterbollen";

        Sample sample = new Sample(targetWord);

        String[] guesses = { "r", "s", "t", "l", "n" };

        for (String guess : guesses) {
            sample.guess(guess);
            System.out.println("After guessing " + guess + ": "
                    + sample.getCurrentResult());
        }
    }
}
</code></pre>
