Java - Cannot Use Class From Same Directory

Viewed 152

I'm trying to call the Game class (that resides in src/main/java/Game.java) from within the Main class (that resides in src/main/java/Main.java), but I'm getting this error:

Main.java:3: error: cannot find symbol Game hangman = new Game(); symbol: class Game; location: class Main::: Main.java:3: error: cannot find symbol Game hangman = new Game(); symbol: class Game; location: class Main::: 2 errors; error: compilation failed

Here is the code that is in the Main.java file:

public class Main {
  public static void main (String[] args) {
    Game hangman = new Game();
    hangman.initialize();
  }
}

Here is the code that is in the Game.java file:

public class Game {
  private String randomWord;
  private int maxGuesses;
  private int guessCount;

  public Game () {
    this.maxGuesses = 8;
    this.guessCount = 0;
  }

  public void incrementGuessCount () {
    this.guessCount++;
  }

  public String generateRandomWord () {
    String[] words = {"puppy", "pool", "avalanche"};
    String result = words[(int) Math.floor(Math.random() * words.length)];
    this.randomWord = result;
    return result;
  }

  public void initialize () {
    String word = this.generateRandomWord();
    System.out.println(word);
  }
}

I'm using WSL, and this is my current Java version.

openjdk 11.0.13 2021-10-19 OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

0 Answers
Related