Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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!