Java - Cannot find symbol constructor
Solution 1
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);
Solution 2
Clearly you are using 0-arg constructor
, when you haven't got one: -
Player player = new Player();
Note that, when you provide a parameterized constructor in your class, the compiler will not add default constructor. You would have to add one 0-arg constructor manually, if you are using it.
So, either you can add one 0-arg constructor
as such: -
public Player() {
this.nick = "";
this.type = "";
this.health = -1;
}
or, use the parameterized constructor to create the object.
Jarand Boge
Updated on June 05, 2022Comments
-
Jarand Boge almost 2 years
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); }
-
Louis Wasserman over 11 yearsYou're trying to create a
Player
, but you're not passing in anickName
or aplayerType
.
-
-
Jarand Boge over 11 yearsWhat I want to do is make the program start, and then prompt the user for a name and nick string. Wont this just name the player and class "My Nickname" and "Player Type"?
-
Jarand Boge over 11 yearsHow can I use the parameterized constructor to create the object? Thats kinda what I was hoping to do. What I want to do is make the program start, and then prompt the user for a name and nick string, without having any values for playerType and nickName before they are entered in the strings. Again, I'm 100% green at this, so If not making any sense, tell me :)
-
Rohit Jain over 11 years@JarandBoge.. Then first read the values from the user. Store it in some variable. And then use the parameterized constructor, passing those variables as parameter. That wouldn't be that difficult. It's just the way you pass string literals as parameter.
-
Jarand Boge over 11 yearsCannot find symbol - class scanner. Am I doing something wrong?
-
Reimeus over 11 yearsYou need to
import java.util.Scanner;