Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Creating the MVP Storing Guesses

hits and misses

By the command hits += letter and same for misses, it is going to concatenate the characters in a random manner. So how is that useful? I mean how will that unjumble into the correct guess?

1 Answer

You can access this large concatenated string by using the indexOf method.

For example:

if (misses.indexOf(letter)) >= 0{
     System.out.println("True");
} else {
     System.out.println("False");
}

If this statement prints "True", we know that the letter given has already been guessed, but is not in the "hits" string.

And for the "hits" string we don't need to know what the word is we just need to guess all the letters inside the word, that is how Hangman works. In short, the computer will only search for the letters inside the word given not the actual word itself. Hope this answer helps!