Search code examples
javamethodsconstructorprogram-entry-point

Java - Cannot find symbol constructor


I'm completely new to Java, so I'm sorry if my question is dumb. Im working on this assignment, and I've been reading about main methods for hours now, but I just cant figure it out. I put some of my code below. I might be way off here, but what I'm hoping to accomplish is to get the main method to start the constructor, but when I compile I get an error saying "cannot find symbol - constructor Player". Now, Im guessing this has something to do with the string parameters of the constructor, but I'm all out. If anyone could shed some light on this, probably very simple problem, I'd be very happy :)

public class Player {
private String nick;
private String type;
private int health;


public static void main(String[] args)
{
    Player player = new Player();
    player.print();
}


public Player(String nickName, String playerType) 
{

    nick = nickName;
    type = playerType;
    health = 100;
    System.out.println("Welcome " + nick +" the " + type + ". I hope you are ready for an adventure!");
}

   public void print()
{
    System.out.println("Name: " + nick);
    System.out.println("Class: " + type);
    System.out.println("Remanining Health: " + health);
}

Solution

  • Player has no no-arg constructor, you could use:

    Player player = new Player("My Nickname", "Player Type");
    

    If you wish to prompt the user for the Player arguments, you can read like so:

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter Player Name:");
    String nickName = scanner.nextLine();
    System.out.print("Enter Player Type:");
    String playerType = scanner.nextLine();
    Player player = new Player(nickName, playerType);