Question

I am doing a hangman project. I have got most of the code working.

The one part I can't get working is where the "secret word" has more than one letter which is the same. For example "hello" has 2 "l"'s.

This is the code for the part of the code where it replaces the "----" (hello) with the letter that was guessed if the guess was right:

int pos = $Input.indexOf($Guessed); 

if (pos == -1) 
{ 
    System.out.print("Search string '" + $Guessed + "' not found"); 
} 
else 
{ 
    System.out.println("\nSearch string found at " + (pos + 1));              
    pos = $Input.indexOf($Guessed);
    String before = $Display.substring(0, pos); 
    String after = $Display.substring(pos + $Guessed.length()); 
    System.out.println(before + $Guessed + after); 

    $Display = before + $Guessed + after;
    System.out.println($Display);            
    $GuessAmt++;
}

I have looked at various answers on here but I cannot get one to work so far. Obviously if someone guessed "l" then "-----" would change to "--ll-" for (hello).

I am not looking for someone to code this for me as I enjoy a challenge but a bit of a hint would be lovely!!

Obviously from looking at my code you may be able to guess we are not allowed to use arrays yet unfortunately. This is only an intro to Java class really.

Any help would be appreciated.

EDIT: Just to be clear, at the moment it ONLY does the first "l" not both the "l"'s of (hello).

EDIT: Changed to this. However, now it is repeating the "inside if" print statement over and over. Cant see how to fix this!

int pos = $Input.indexOf($Guessed); 

        if (pos == -1) 
        { 
            System.out.print("Search string '" + $Guessed + "' not found"); 
        } 
        else 
        { 
            //System.out.println("\nSearch string found at " + (pos + 1)); 

            for(int i=0;i<$StrLength;i++)
            {
                System.out.println(i);
                if($Input.charAt(i) == $Guessed.charAt(0))
                {
                    i = $Input.indexOf($Guessed);
                    String before = $Display.substring(0, i); 
                    String after = $Display.substring(i + 1); 
                    System.out.println("inside if" + before + $Guessed + after); 
                    $Display = before + $Guessed + after;
                }
            }



                System.out.println($Display);



            $GuessAmt++;
        }

No correct solution

OTHER TIPS

If you still wanna use the indexOf you can use its overloaded version as well to insure that you have gone through all letter occurrences:

int index = str.indexOf('l');
while(index  >= 0) {
     // FILL THE BLANKS WITH THE LETTER
     index  = str.indexOf('l', index +1);
}

You can iterate over the String and check every character individually using word.charAt(i).

for (int i = 0; i < word.length; i++) {
    if (word.charAt(i) == guessedChar) {
        // do stuff
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top