Hello, all. I'm creating a program that lets a user play Hangman. I've attached the directions for the program for clarification. My code is pasted below. I've set up a couple methods for it, but I'm having difficulty in understanding how to call these methods in the main class (assuming my methods are set up correctly to begin with).

package hangman;

import java.util.Scanner;

/**
 *
 * @author Andrew
 */
public class Hangman
{
    int MistakesAllowed = 6;
    String SecretWord;
    int NumberOfMistakes;
    int Chances = MistakesAllowed - NumberOfMistakes;
    String rightLetters = "";

    public void newGame()
    {
        rightLetters = "";
        NumberOfMistakes = 0;
        System.out.println();
        System.out.println("* * * * * * * * * * NEW GAME * * * * * * * * * *");
        System.out.println();
        System.out.println("Player 1, please enter a secret word: ");
        Scanner input = new Scanner(System.in);
        SecretWord = input.nextLine();
        System.out.print("The word is: ");
        for (int i = 1; i <= SecretWord.length(); i++)
        System.out.print("_");
        System.out.println();
        System.out.println();
        System.out.println("Player 2, you have 6 chances to find the word.");
        System.out.println("Use method \"guess(char c)\" to guess a character");
        System.out.println("or use method \"giveUp()\" to quit");
    }

    public void giveUp()
    {
        System.out.println();
        System.out.println("* * * * * * * * * * * * PLAYER 2 GIVES UP * * * * * * * * * * *");
        System.out.println();
        System.out.println("The hidden word was " + SecretWord);
        System.out.println();
        System.out.println("Use method \"newGame()\"to start a new game.");
    }


    public void guess(char c) 
    {
        // if the guess isn't a letter, tell the user and return
        if (!Character.isLetter(c))
        {
            System.out.println("Only enter letters!");
            return;
        }

        // If the guess is wrong:
        if (SecretWord.indexOf(c) == -1)
        {
            NumberOfMistakes++;
            System.out.println("Wrong!  Try again!");
            System.out.println("Your remaining chances are " + Chances);

            if (NumberOfMistakes == MistakesAllowed)
            {
                System.out.println();
                System.out.println("* * * * * * * * * * * * PLAYER 2 LOSES * * * * * * * * * * *");
                System.out.println();
                System.out.println("The hidden word was " + SecretWord);
                System.out.println();
                System.out.println("Use method \"newGame()\"to start a new game.");
            }
        } 

        else // The guess is correct
        {
            System.out.println("Correct!");
            rightLetters = rightLetters + c;

            for (int i = 0; i < SecretWord.length(); i++) 
            {
                char ch = SecretWord.charAt(i);
                if (rightLetters.contains(String.valueOf(ch))) 
                {
                    System.out.print(ch);
                }
                else 
                {
                    System.out.print("-");
                }
            }
        System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        newGame game = new newGame();
    }
}

I dont have any errors displayed in the code I've writen, but when I run it I get:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
  symbol:   class newGame
  location: class hangman.Hangman
    at hangman.Hangman.main(Hangman.java:98)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Any direction would be greatly appreciated. Thanks!

Because newGame() is a method, not a class, and you are trying to declare an object of type "newGame". Simply change the code in your main() method to this instead.

newGame();

I tried that earlier. When I enter it as you've shown:

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

I get the following error:

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static method newGame() cannot be referenced from a static context
    at hangman.Hangman.main(Hangman.java:98)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Not going to go into much details here. But to make your program work (so you can see the output and continue developping it, this can't be your final version) replace your main method with this:

public static void main(String[] args) 
    {
        Hangman game = new Hangman();
        game.newGame();
    }

Sound advice Seldar, but perhaps you could now explain for Andrew's benefit why he had that error and why that fixes it?

Most certainly I will.

Put in simple words, the method newGame() belongs to the class Hangman and can only be accessed from objects of the same type, hence the "." (dot) operator. Therefore before you are able to call newGame, you need to have an object of type Hangman.

Hangman game = new Hnagman();
game.newGame(); //now you start the game on the new object

You had an error because you were trying to call a non-static method (one that has be to invoked by an instance of a class) when you hadn't created the obeject in advance of that invocation.

A static method on the other hand is one which does not need an object in order to be invoked, it can be accessed through your class.
For example, if the newGame(); method had the keyword static in its declaration your would have been able to call it as follows:
Hangman.newGame(); //static call

Hope this helps.

commented: Excelent explanation, very helpful +15

That got it working. Thank you very much, Seldar!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.