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 trialGrigorij Schleifer
10,365 PointsWhy we return isHit in the Validation challenge?
This method make me feel my overheated brain :)
public boolean promptForGuess () {
Console console = System.console();
boolean isHit = false;
boolean isValidGuess = false;
while(! isValidGuess){
String guessAsString = console.readLine ("Enter a letter: ");
char guess = guessAsString.charAt(0);
try{
isHit = mGame.applyGuess(guessAsString);
isValidGuess = true;
}catch (IllegalArgumentException iae){
console.printf ("%s. Please try again.\n", iae.getMessage());
}
return isHit;
}
}
I dont understand why we delete
return mGame.applyGuess(guessAsString);
and replace it with
return isHit;
What i donΒ΄t understand exactly, is that the isHit boolean is false .... why we return a false boolean???
3 Answers
Craig Dennis
Treehouse TeacherIn the try block we set it
isHit = mGame.applyGuess(guessAsString);
... and we keep setting it until it a valid guess is submitted.
Make more sense?
Grigorij Schleifer
10,365 PointsHi Craig,
is it normal, that i am almost lost in this tutorial. The challenges are great and i pass them with normal effort. But i dont undetstand a lot in the tutorial. I am a programming beginner.
James Moran
7,271 PointsI don't think you actually need to return anything anymore. I had the same issue as you, what does the return actually do:
public void promptForGuess(){
Console console = System.console();
boolean isHit = false;
boolean isValidGuess = false;
while(!isValidGuess){
String guessAsString = console.readLine("Enter a new letter: ");
char guess = guessAsString.charAt(0);
try{
isHit = mGame.applyGuess(guess);
isValidGuess = true;
}
catch(IllegalArgumentException iae){
console.printf("%s. Please try again.\n", iae.getMessage());
}
}
}
foxyrev91
3,912 Pointsfoxyrev91
3,912 PointsBut what is the purpose of returning it at the end of the method?