2

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)

3 Answers 3

11

You're missing the new keyword. e.g.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

wow. I don't even know how both I and the person sitting next to me missed that @_@
A good IDE, like IntelliJ, would have found it faster than both of you.
2

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.

Comments

2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.