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 Prompting for Guesses

gurveer aulakh
gurveer aulakh
2,800 Points

Prompter.java

in the prompter class what is the use of the below code in PromptForGuess method: char guess = guessInput.charAt(0); As I got confused on that line because why are we asking to get the first letter of the word.

2 Answers

Hello

if the confusion is related to the use of 0 in this like

char guess = guessInput.charAt(0);

Then this is because java is zero(0) based index language ... same a s C and C#,

therefore, if I have an array of 3 elements ,,, le cally myArry[3]. the the array contents are:

myArry[0]. myArry[1], myArry[2]

you can think of a string a an arry of Char. Therefore is a have a string "this is my string" and I wan to get the first char in this string, then I would

char myChar= "this is my string".charAt(0);

myChar will hold the letter 't'

Hope this helps clarifies

Thanks

Mustafa Alordowny
Mustafa Alordowny
4,495 Points

this line is used to assign the value of the first letter in the string guessInput to the character named guess.

gurveer aulakh
gurveer aulakh
2,800 Points

but why are we assigning the value to only to the first letter.As don't we need to assign to the whole word.

Mustafa Alordowny
Mustafa Alordowny
4,495 Points

because the concept of Hangman game is to take a letter from the player as a guess. the player has to guess each letter of the word separately.