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 (Retired) Delivering the MVP Validation

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Why 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
STAFF
Craig Dennis
Treehouse Teacher

In 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?

foxyrev91
foxyrev91
3,912 Points

But what is the purpose of returning it at the end of the method?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

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

I 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());
      }
    }
  }