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 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.