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 Creating the MVP Prompting for Guesses

Public boolean promptForGuess ?

Hello treehouse

I didn't get this methods Public boolean promptForGuess...why we return boolean?

it start to be confuse a little please explain it to me!

1 Answer

Mihai Craciun
Mihai Craciun
13,520 Points

well let's explain the code method by method

first we have the applyGuess method that looks like this

public boolean applyGuess(char letter) {
boolean isHit = answer.indexOf(letter) != -1;
if(isHit){
   hits += letter;
}else {
  misses += letter;
}

this method will check if the letter we pass in this method is in the answer String and return whatever this is false or true

secondly we have the promtForGuess method

public boolean promptForGuess(){
   Scanner scanner = new Scanner(System.in);
   System.out.print("Enter a letter: ");
   String guessInput = scanner.nextLine();
   char guess = guessInput.charAt(0);
   return game.applyGuess(guess);

this method ask the user to enter a letter in keyboard and then check if the letter is correct by appealing the applyGuess method. Because we return the value of a method the method type should be same as the method we call. In the main method we call a new Prompter object and then we call the promptForGuess() method that will ask for a letter and then check if it is correct by calling the applyGuess(char letter) method and the value this method returns will go back to main to be used as we wish. It's like method are chained together to solve a problem and every single one of them solve a little bit.

Oh now i got this thank you very much! :)