1

im new to java so I imagine this is a very simple question but I cannot find my answer

Im creating a very simple game but when i come to compile my main I get

 BattleShipGame.java:19: error: cannot find symbol
 BattleShip ship = new BattleShip();
        ^
 symbol:   class BattleShip
 location: class BattleShipGame

 BattleShipGame.java:19: error: cannot find symbol
 BattleShip ship = new BattleShip();
                          ^
  symbol:   class BattleShip
  location: class BattleShipGame
  2 errors

So when i go to create my object in the main it cannot find the symbol and create the object

My battle ship class :

public class BattleShip {

//delcare an int arry to hold the location of the cells 
private int[] location;

//setter for location 
public void setLocation(int[] shipLocation){
    location = shipLocation;
}

public String checkGuess(String[] g){

//return the message
return message;
}

}

Main method :

public class BattleShipGame {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    //create a battle ship object 
    BattleShip ship = new BattleShip();
    //hard code location of ship
    int[] ShipLocation = {4,5,6};
    //set the location of the object
    ship.setLocation(ShipLocation);
    //take the users guess from command line
    String[] guess = {args[0], args[1], args[2]};
    //take message returned from method
    String message = ship.checkGuess(guess);
    // print out the message
    System.out.println(message);

    }
 }

If anyone could let me know why i cant create an object?

I compiled the battleship class before the main These are both in the same package do I still have to import?

Aidan Gee
  • 578
  • 1
  • 11
  • 26

7 Answers7

2

You need to make sure about two things:

  1. You should compile your BattleShip class first before using it in your BattleShipGame game
  2. If both BattleShip and BattleShipGame classes are not in the same package then you need to import the BattleShip class in your BattleShipGame class using import statements.
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Imports are missed.

   //imports missing here

public class BattleShipGame {
    public static void main(String[] args) {

        //create a battle ship object 
        BattleShip ship = new BattleShip();   

To Use that object you have to import

I suggest you to use an IDE that corrects your compile time errors and saves a lot of time

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

You forgot import com.your.package.BattleShip in BattleShipGame.java.

Use for example Eclipse or any other IDE, which will manage imports for you. In Eclipse the shortcut is Ctrl+Shift+O.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
0

You have to put both file in a single directory. Suppose you put both files in different directory say Game and Main. then in main file add this line in starting import Game.BattleShip; and add this line in BattleShip class file in first line package Game;

This will solve your problem.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
0

Write booth classes in same directory or use import statement in BattleShipGame class

import com.test.BattleShip;

public class BattleShipGame {
   public static void main(String[] args) {
     BattleShip ship = new BattleShip();
   }
}
king_nak
  • 11,313
  • 33
  • 58
Jamsheer
  • 3,673
  • 3
  • 29
  • 57
0

Im not sure quite what was wrong I moved the files out of the netbeans created project and it all worked fine , thanks for your answers though

Aidan Gee
  • 578
  • 1
  • 11
  • 26
0

Try to run code using right click on Main.java and click run instead of compile using terminal. Otherwise you need to compile other classes before main class.

Isuru
  • 21
  • 5
  • Their filename is not Main.java. Also, they've mentioned in one of the comments that they are using netbeans IDE which compiles the code automatically and no action is required. – v0ld3m0rt Jun 28 '22 at 11:33