0

When I compile I am getting the error: cannot find symbol symbol : constructor Team()

public class Team {
    public String name;
    public String location;
    public double offense;
    public double defense;

    public Team(String name, String location) {

    }     

    public static void main(String[] args) {

        System.out.println("Enter name and location for home team");
        Scanner tn = new Scanner(System.in);
        Team team = new Team();
        team.name = tn.nextLine(); 

        Scanner tl = new Scanner(System.in);
        team.location = tl.nextLine();
    }
}

Any ideas how to fix? many thanks Miles

Miles Roberts
  • 415
  • 5
  • 7

6 Answers6

5

If you explicitly create any constructor in class, compiler does not provide default constructor. You need to create no-args constructor explicitly.

If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program.

Read more on Documentation

Updated Code -

public class Team {
    public String name;
    ...
    public Team(){...}
    public Team(String name, String location) {...}   
    ...
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
2

provide default constructor to resolve error

jmj
  • 237,923
  • 42
  • 401
  • 438
1

You need to add a non-parameter constructor to your class as well. As you are trying to call the non-paremeter constructor here:

    Team team = new Team();

Just add this to your class also:

public Team() {

}   

Generally when there is no constructor defined in a class then compiler adds a default non-parameter constructor to it. But if any non-default constructor is added then compiler will not add the default non-param constructor. That need to be added manually as mentioned above.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

In your Team class, there is no default constructor, but you created object using of it. Try,

public class Team {
    public String name;
    public String location;
    public double offense;
    public double defense;

    public Team(){}

    public Team(String name, String location) {

    }     

    public static void main(String[] args) {

        System.out.println("Enter name and location for home team");
        Scanner tn = new Scanner(System.in);
        Team team = new Team();
        team.name = tn.nextLine(); 

        Scanner tl = new Scanner(System.in);
        team.location = tl.nextLine();
    }
}

If you have other constructors you must define a no-arg constructor by yourself as Java only provides a default no-argument constructor if no other constructors are defined.

gjman2
  • 912
  • 17
  • 28
1

No argument constructor nor valid here. Since you are override it by following

public Team(String name, String location) {

} 

So you can't do Team team = new Team(); .

instead of this try

      Team team = new Team("Name","Location");

since your constructor only accept two String arguments. Or you can add no argument constructor to your Team class.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

The constructor you defined is public Team(String name, String location) which takes two arguments.

However, the constructor you called is Team() which takes no arguments, which means it is a different constructor from Team(String name, String location), as they have different arguments list.

You should define a Team() too if you want to create an empty Team object

taffykula
  • 566
  • 7
  • 8