-3

Hi i'm doing my assignment, i'm making a tic tac toe game. i'm using notepad for this purpose from my teacher instructions, i'm making 2 classes in a single notepad, a main class of TicTacToe name or 2nd GameBoard. everything is alright in netbeans but when i implemented in notepad and execute it through cmd then there is error

TicTacToe.java:16: error: class GameBoard is public, should be declared in a file named GameBoard.java`

`

public class GameBoard {

private char[][] gameBoard; 

 public GameBoard() 
{ gameBoard= new char[3][3];
 for(int row=0; row<gameBoard.length; row++)
 {
java.util.Arrays.fill(gameBoard[row], ' ' );
}
    }
      public void displayBoard()
    {
for(int row=0; row<gameBoard.length; row++)
 {
for(int col=0; col<gameBoard.length ; col++)
{
System.out.print("\t" + gameBoard[row][col]);
 if(col==0 || col==1)
System.out.print("|");

  }
 System.out.print("\n---------------------------\n");
 }
  }

  }

This is my code

  • Notepad is not creating a .java file for you. You'll need to rename the .txt file's extension. When using an IDE like NetBeans, it does this for you. – Keith Sep 01 '15 at 19:09
  • but i'm using already named TicTacToe.java – user3668974 Sep 01 '15 at 19:12
  • read the answers -- namely, go see the Oracle documentation provided in my answer for direct guidance. – Keith Sep 01 '15 at 19:19

3 Answers3

3

You can only have one public class in a single Java file, and the name of the Java file has to match the name of the public class inside it. Some possible solutions:

Option 1 - Pull the GameBoard class into a new file and name the file GameBoard.java. This means that you also need to change the extension of the file. Make sure that the files are in the same folder, unless you want to add an import statement.

Option 2 - Remove the modifier (public) of the GameBoard class (defaults to package-private)

Option 3 - Nest the GameBoard class inside your TicTacToe class (you can still change/remove the modifier if you want to; public nested classes are usually not recommended)

Zarwan
  • 5,537
  • 4
  • 30
  • 48
  • if i changed the name then new error will be tictactoe should be declared etc – user3668974 Sep 01 '15 at 19:13
  • i'm using 2 classes in a single notepad , main class is TicTacToe and notepad also name is TicTacToe.java , and other class is GameBoard class, here is error exact TicTacToe.java:16: error: class GameBoard is public, should be declared in a file named GameBoard.java` – user3668974 Sep 01 '15 at 19:17
  • private modifier not allowed here, and if i make a new file then how to access it from main file? – user3668974 Sep 01 '15 at 19:20
  • @user3668974 I updated my answer to make it more clear. Option 2 is the simplest but all 3 should solve your problem. – Zarwan Sep 01 '15 at 19:33
2

You cannot declare two public classes within a single file (unless they are nested). Right now you have defined TicTacToe and GameBoard in the same .java file.

Read this tutorial on class declarations

Keith
  • 3,079
  • 2
  • 17
  • 26
0

Only 1 public class allow per file (TicTacToe.java) so you need to rename it. You can make the other class with no access modifier :

public class TicTacToe {
    public static void main(String[] args) {
        GameBoard board=new GameBoard();
        board.displayBoard();
    }
}
class GameBoard {
    private char[][] gameBoard;
    public GameBoard() {
        gameBoard = new char[3][3];
        for (int row = 0; row < gameBoard.length; row++) {
            java.util.Arrays.fill(gameBoard[row], ' ');
        }
    }
    public void displayBoard() {
        for (int row = 0; row < gameBoard.length; row++) {
            for (int col = 0; col < gameBoard.length; col++) {
                System.out.print("\t" + gameBoard[row][col]);
                if (col == 0 || col == 1)
                    System.out.print("|");
            }
            System.out.print("\n---------------------------\n");
        }
    }
}
chenchuk
  • 5,324
  • 4
  • 34
  • 41