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

Michelle Miller
Michelle Miller
610 Points

Prompt For Guesses: What purpose does the Console object serve?

Why is it required for the promptForGuess variable?

1 Answer

I am not sure what you mean by "promptForGuess variable". In the video at around time 4:37, I saw this code that I think you are referring to:

public boolean promptForGuess() {
  // promptForGuess is the name of this method that you are writing, creating

  Console console = System.console();
  // Console is something Java already wrote for people to use to get input and print output.
  // There are other ways to get input from the user, but this is the simplest for beginners.

  String guessAsString = console.readLine("Enter a letter: ");
  // console reads the input the user types in and stores it inside a string variable (guessAsString)

  char guess = guessAsString.charAt(0);
  // this line gets the 1st letter of the input. So if the user typed "No", guess is "N".
  // if user typed in "yes", then guess is "y"
  // if user typed in "Apple", then guess is "A"

  return mGame.applyGuess(guess);
  // this checks to see if the letter was in the hangman word
}
// he calls the method later in the video in another piece of code
public static void main(String args[]){
 ...
    Prompt prompter = ...;
    // create a prompter object

    boolean isHit = prompter.promptForGuess();
    // call the promptForGuess method that we wrote and store the result in variable isHit
 ...
}

My advice is to use the website's workspaces and start typing and follow along with the code. Here's an example from my workspace: https://teamtreehouse.com/workspaces/16125232