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

I need some help. I might have missed something.

I might have missed something in an earlier lesson. I am in the refactoring video. I am getting the following error when I try and run my code:

Error: Main method not found in class Prompter, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

The error message is quite clear, do you have a main-method with the specified signature in your Prompter class? If you do, please provide us the code so we might see what else could cause the issue.

import java.io.Console;

public class Prompter {
  private Game mGame;

  public Prompter(Game game) {
   mGame = game; 
  }

public boolean promptForGuess()  {
  Console  console = System.console();
  boolean isHit = false;
  boolean isValidGuess = false;
  while (! isValidGuess) {
   String guessAsString = console.readLine("Enter a letter:  ");
    try {
     isHit = mGame.applyGuess(guessAsString); 
     isValidGuess = true;

    } catch (IllegalArgumentException iae) {
     console.printf("%s. Please try again. \n", iae.getMessage());
    }
  }


  return isHit;
}

  public void displayProgress () {
    System.out.printf("Try to solve: %s\n", mGame.getCurrentProgress());
  }

}

This is what I have right now.

The error asks you to add a method like this:

public static void main(String[] args) { }

I am not familiar with this specific challenge so I do not know whether soemthing is supposed to be inside this method, but it has to exist nonetheless. The main method is the standard point of entry for your program.

Prompter.java:3: error: class, interface, or enum expected
public static void main(String[] args) {

Thats the error I'm getting now. I am assuming the code that I copied has to be inside those parameters. I still get this error though.

Your code does not have to (in fact, must not) be inside the main method. It is a method like the other ones you already have and must be located on the same level. For example, right after your constructor but before the promptForGuess()-method.

Can you link the challenge or video you are talking of? It will make it easier for others to help you.

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Benjamin, the you should be running java Hangman do you have that file? That has the main method.

I got it to work after running Hangman! Thank you everyone for the response! Great teaching Craig! I never knew I could learn programming like this!

Even I'm getting the same error. Also sometimes System.out.println (" Text"); is not working properly. If I try printf instead of println, it works.