I’m trying to write a piece of code that allows me to type in a letter and then checks if the letter belongs to the word. Then it should display the word with only the correct letter visible.
Example:
Word that I need to guess : jungle book
as displayed on screen: ***** ****
letter that I am guessing: j
display on screen: j**** ****
and so on
What I’ve got so far:
public void guessConsonent() {
String guessedConsonent = consonentInput();
// returns a letter
wordInStars = "";
for (int s = 0; s < secretWord.length(); s++)
if (secretWord.substring(s, s+1).equals(guessedConsonent)) {
wordInStars += guessedConsonent;
} else if (woordVanCat.substring(s, s+1).equals(" ")) {
wordInStars += " ";
} else {
wordInStars += "*";
}
System.out.println(wordInStars);
}
The problem is that it does not add the consonent to the word even if it’s correct. I still only get ‘ ***’
regards
It seems you are writing a hangman-clone in Java. The snippet I don’t see is: