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

help me

What am I doing wrong?

ERROR

Hangman.java:9: error: non-static method promptforGuess() cannot be referenced from a static context                                                                                                                                           
    boolean isHit = Prompter.promptforGuess();                                                                                                                                                                                                 
                            ^                                                                                                                                                                                                                  
1 error ;

Here is my program:

In Hangman.java:

public class Hangman {

  public static void main(String[] args) {
    // Your incredible code goes here...

    Game game = new Game("Treehouse");
    Prompter prompter = new Prompter(game);

    boolean isHit = Prompter.promptforGuess();

    if(isHit){
      System.out.println("You did it!");
    }
    else{
      System.out.println("You missed it");
    }




  }
}

In Prompter.java

import java.util.Scanner;

class Prompter{

  private Game game;

  public Prompter(Game game){
    this.game = game;
  }

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

Please help me.

1 Answer

You are trying to call a static method on the Prompter class instead of calling the method on the instance of Prompter that you created. To fix this, just make the 'p' lowercase:

boolean isHit = prompter.promptforGuess();