简体   繁体   中英

Java - symbol not found (constructor)

So I'm writing some code that reads from a file:

array[k] = Salesperson(infile.nextInt(), infile.nextInt(), myName);

I wrote a constructor for Salesperson that looks somewhat likes this:

public Salesperson(int cheese, int butter, String name)

When I try to compile (first Salesperson, then the actual program), I get this:

program.java:39: cannot find symbol

symbol : method Salesperson(int,int,java.lang.String)

You're missing the new keyword. eg

array[k] = new Salesperson(infile.nextInt(), infile.nextInt(), myName);

This is resulting in the compiler attempting to find a method called Salesperson that returns a type of Salesperson, which would be invalid anyway.

Use the new keyword. You should do it:

array[k] = new Salesperson(infile.nextInt(), infile.nextInt(), myName);

You can't assign without the new keyword because it's not a method where you can return a value.

As I see it, you have declared an array of Salesperson objects and you want to put data into it from a file. What you are missing is the new keyword. Using new keyword creates a new object of the class and calls the constuctor in the process. You may use the follwing code:

array[k] = new Salesperson(infile.nextInt(), infile.nextInt(), myName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM