do {
userInput = input.nextLine().toUpperCase(); // Convert input to uppercase to match secretWord's case
if (userInput.length() > 5) {
System.out.println("Invalid Guess, word is too long");
} else if (userInput.length() < 5) {
System.out.println("Invalid Guess, word is too short");
} else {
for (int i = 0; i < 5; i++) {
String userChar = userInput.substring(i, i + 1);
String secretChar = secretWord.substring(i, i + 1);
if (userChar.equals(secretChar)) {
System.out.print(GREEN + userChar + RESET);
} else if (secretWord.contains(userChar)) {
System.out.print(YELLOW + userChar + RESET);
} else {
System.out.print(RED + userChar + RESET);
}
}
System.out.println();
if (userInput.equals(secretWord)) {
correct = true;
}
guessCounter++;
}
} while (guessCounter < maxGuess && !correct);
The program starts by choosing a word from an array of words. say the word selected from the array is Brain, if the user were to type in iiiii it would display the first 3 in yellow the 4th in green and the 5th in yellow. I need it to display all but the 4th in red to show that those letters are not in the word.
I've tried taking the index of userChar and comparing it to the index of secretChar which results in the same thing happening. is there a way I could use something like secretWord.contains(userChar) to determine that it should be red? any help would be appreciated!
When marking yellow letters, you need to keep track of which letters were already "claimed" as yellow or green. One approach is to keep a list (or multiset) of available chars and empty it as letters are claimed. You'll also have to double-check that you're not stealing a green in advance. I think the easiest way to do that is to run through the word twice, first claiming the green letters, then again to print: