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!

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

Kevin Michie
Kevin Michie
6,554 Points

How did we iterate through in Prompter class?

When the promptForGuess was made, from what I understand, my guessAsString variable stored the user input and placed the value in the first position, position 0, of guess.

When we called 't' to check if that character was in the game string, my brain thought that, "Oh, 't' is a Character in the 0 index and that we printed out, 'We got a hit!' because t is in that position!" Then when we ran again that z does not work, I assumed that right now we can only identify what character is at index of 0.

I tested to see if 'r' would then work because r is the index of 1, which means I should return a MISS for right now. When I checked, it showed we have a HIT! instead.

Am I iterating over the string using the applyGuess class as it checks if the index is greater than 0?

3 Answers

Andrew Winkler
Andrew Winkler
37,739 Points

Okeydoke, I'm just going to go over the logic of the video step by step, because somewhere you're missing a piece of understanding. Here we go:

import java.io.Console; //this line of code is needed to import the method that prompts for user input

/* Let's first get her prompter able to accept guesses.
    To do this we have to add a constructor that accepts the game logic object 
   (which we have already created) */
public class Prompter {
  private Game mGame; //we are storing the game logic in a private variable called mGame

  public Prompter(Game game) {
    Game = game;

// then Craig adds a prompter to get the guess from the user via a console prompt
  public boolean promptForGuess() {
    Console console = System.console();
    // this is the method that prompts for user input requiring import java.io.Console

   String guessAsString = console.readLine("Enter a letter: "); 
   //stores prompted user input as a String named guessAsString

  char guess = guessAsString.charAt(0);
  /* This is likely where you got confused. By nature of the prompt design the user could 
enter multiple letters such as 'xyz', but we're asking them for one character as a guess. 
How do we simplify this ad dodge errors? By appending .charAt(0) to whatever the user 
input is, the program will only take the first character they enter as their guess - all of the 
others will be ignored. */

  return mGame.applyGuess(guess);
  /* this will return true or false if the first character of the user guess equals a character 
from the compared to string from hangman.java (**The iteration method of which is defined in the game logic!!!**) */

To explain charAt() in depth: this method returns the character located at the String's specified index. The string indexes start from zero. Here is the syntax of this method: public char charAt(int index). An example of this would be:

public class Test {

   public static void main(String args[]) {
      String s = "Strings are immutable";
      char result = s.charAt(8);

Which would print out: a

Moving onto completing the hangman class:

public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code here:
      Game game = new Game("treehouse");
      Prompter prompter = new Prompter(game);
     //inject game logic

     //set outputs based on game logic
      boolean isHit = prompter.promptForGuess();
      if (isHit) {
        System.out.println("We got a hit!");
      } else {
        System.out.println("Whoops that was a miss");

If you don't understand the true or false ouputs of the game logic, you need to review that earlier video and challenge (that's the only thing I haven't covered in this post. Hope this cleared some things up.

Andrew Winkler
Andrew Winkler
37,739 Points

you filled this question under the wrong forum hierarchy then. It's probably best to re-post the question in the right place. Look at the top of the screen and you'll see "Java > Java Objects > Creating the MVP > Prompting". You've mistakenly posted this as a java question.

Kevin Michie
Kevin Michie
6,554 Points

Sorry I should have been more clear. In python, the moment you find your character to add, the method breaks. I'd have to Code that even after it finds the character to go to the next position and continuing the search until the end of length. For example, when we run e through for 'treehouse', '--ee----e' is the result rather than just '--e------'. I'm curious as to how the code iterates through without adding a code to continue searching after finding.