private String theLetters = "_ _ _ _ _\n";
StringBuilder myName = new StringBuilder(theLetters);
for(char e : theSecretWord.toLowerCase().toCharArray())
{
if(e == theUsersGuess.charAt(0))
{
int index = theSecretWord.indexOf(e) * 2;
myName.setCharAt(index, theUsersGuess.charAt(0));
theLetters = myName.toString();
}
}
For some reason this will only replace the first occurrence of a letter from the String theSecretWord and not the second, even though this for each loop goes through each character and replaces it in theLetters accordingly. I don't understand why it won't replace more than one occurrence of a letter.
I think it's because the loop stops once it finds a matching letter even though it shouldn't.