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 trialEric Tang
Courses Plus Student 1,300 PointsguessInput.charAt(0)
Hey guys, for the most part I understand what is going on. However one part that I can't wrap my head around is the guessInput.charAt(0). Why is the number set to 0. Does that mean that it checks for the first letter. When prompt for letter, I typed in the third number but it still hit. Can someone explain to me why it still hit when I've set the charAt to 0 which is the first letter.
3 Answers
andren
28,558 PointsguessInput
is the variable that holds the input of what you typed when you were prompted to "Enter a letter". So the 0 refers to extracting the first letter of that input.
Which means that if you contradicted the instructions and enter a word like "Hello" instead of just a letter, then only the first letter of that word "H" would actually be checked against the Hangman word.
Eric Tang
Courses Plus Student 1,300 PointsWow thank you so much. That helped a lot
Patryk Moros
819 Pointsandren you are my hero :) thanks for such a great explanation :)
Eric Tang
Courses Plus Student 1,300 PointsEric Tang
Courses Plus Student 1,300 PointsThat makes so much more sense Thank You. If you don't mind can you also explain the return at the end of each Boolean scope. Why do we need to have a return there and also could you give me an example of how return can be used.
andren
28,558 Pointsandren
28,558 PointsBy "Boolean scope" I assume you refer to the methods that have
boolean
as their return type.return
is used to pass a value back to the code that called the method. Returning values is a fundamental part of Object Oriented Programming but it's not the simplest concept to explain.A method always has to return a value whose type matches the type defined in it's declaration, which is known as it's return type. Though if the return type is defined as
void
then that means that the method does not return anything.Since the
applyGuess
and thepromptForGuess
methods are defined with a return type ofboolean
they have to return a boolean, theapplyGuess
methods returns the contents of theisHit
variable, which means that it will returntrue
if the guess was correct andfalse
otherwise.The
promptForGuess
method returns the result ofapplyGuess
, which means that it basically just passes along whatever the return value ofapplyGuess
is.The point of returning a value is that you can make use of that value in other places in your code, it allows you to not only ask a method to perform some kind of action, but to also give you back the result of said action.
In this case the return of the
promptForGuess
method is used in this line of the Hangman.java file:boolean isHit = prompter.promptForGuess();
When you set a variable equal to calling a method you actually set the variable equal to whatever said method returned to you.
This allows you to make use of the result of calling
promptForGuess
within the Hangman.java file. If the method did not return anything then you would have no way of knowing what the result of calling the method actually was.return
can be used to return any type of value (as long as the return type of the method matches) and on the receiving end it can be used in any way that type of value normally would be.You will be seeing plenty more examples of
return
being used though the Java courses since it is such a fundamental part of the language (and programming languages in general), so you don't have to worry too much if you still don't really get how it works or why it is used. After seeing it in use more I'm sure the point of it will eventually click for you.But if you have any more concrete question about it, or about Java in general then feel free to ask me, I'll answer anything I can.