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 (Retired) Creating the MVP Prompting for Guesses

What is the significance of this line "char guess = guessAsString.charAt(0)"

char guess = guessAsString.charAt(0)

However if i compile and im asked to write a letter, even write a letter, a char not at zero for example 'h' it still gives a hit. so what was the significance of writing zero as if it would only give a hit if i write 't'

1 Answer

Stephen Bone
Stephen Bone
12,359 Points

Hi Blessing

This is just to make sure that the user's guess is a char as that is the argument that the applyGuess method in the Game class expects.

In reality you may want to test if the guess is a char and if it's not ask the user to guess again with a valid guess rather than just strip their guess down to the first letter. Craig may only do it at this at stage as a test although I don't remember if this is rewritten later (I suspect it is).

And yes any letter that is in the answer string will be returned as a hit. If you follow along with what gets called this is the behaviour of the applyGuess method which will check if the guess is in the answer with this line below:

boolean isHit = mAnswer.indexOf(letter) >= 0;

return True or False to the promptForGuess method (in the Prompter class) which then in turn is returned and stored in the isHit variable (in the Hangman class) which is then evaluated by the if statement.

Not entirely sure that made sense but I hope it helped a little.

If you need any further help let me know.