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 trialedmond habimana
Courses Plus Student 8,352 PointsQuestion on applyGuess() method
I have a question on applyGuess() method that accepts a string as a parameter
public boolean applyGuess(String letters){
if(letters.length() == 0){
throw new IllegalArgumentException("No letter found");
}
return applyGuess(letters.charAt(0));
}
Correct me if i am wrong but is the last line of code suppose to return "True" or "False" since the method only accepts a return of "True" or "False"?
the way i am reading that last line is letters.charAt(0) locates the character within a String called "letter" by its index number and passes that character to "applyGuess(char letter)" and also returns that character.
2 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsThe purpose of this method is to leave the logic of the game to the game.
User input is ALWAYS a String. That is how it works in Java.
But for our Game in order to check user input applyGuess
we need only one char
, not the whole String.
That is why we create applyGuess(String letters)
method, that can check String for
emptiness, or do a lot of other stuff,
BUT at the end it will call applyGuess
with
char that is the first letter. Because at the end we still use check one character.
Correct me if i am wrong but is the last line of code suppose to return "True" or "False" since the method only accepts a return of "True" or "False"?
Yes when you write boolean
you HAVE to return true
or false
the way i am reading that last line is letters.charAt(0) locates the character within a String called "letter" by its index number and passes that character to "applyGuess(char letter)" and also returns that character.
Not exactly.
that last line is letters.charAt(0) takes FIRST character of a String parameter called "letter" and passes that character to "applyGuess(char letter)" method, that returns true
or false
.
That last method is base of our Game logic ...
// They work together
// this one is called in our Game
public boolean applyGuess(String letters){
if(letters.length() == 0){
throw new IllegalArgumentException("No letter found");
}
return applyGuess(letters.charAt(0));
}
// this one is used behind the scenes. Can be made private as well I think
public boolean applyGuess(char letter) {
// code
// in the end return "true" or "false"
}
edmond habimana
Courses Plus Student 8,352 Pointsi understand it now!! Thank you so much for the help!