views:

111

answers:

5

The error i'm getting is in the fillPayroll() method in the while loop where it says payroll.add(employee). The error says I can't invoke add() on an array type Person but the Employee class inherits from Person so I thought this would be possible. Can anyone clarify this for me?

import java.io.*;
import java.util.*;

public class Payroll
{   
    private int monthlyPay, tax;
    private Person [] payroll = new Person [1];

        //Method adds person to payroll array
    public void add(Person person)
    {
        if(payroll[0] == null) //If array is empty, fill first element with person
        {
            payroll[payroll.length-1] = person;
        }
        else //Creates copy of payroll with new person added
        {
            Person [] newPayroll = new Person [payroll.length+1];
            for(int i = 0;i<payroll.length;i++)
            {
                newPayroll[i] = payroll[i];
            }
            newPayroll[newPayroll.length] = person;
            payroll = newPayroll;
        }
    }


    public void fillPayroll()
    {
        try
        {
            FileReader fromEmployee = new FileReader ("EmployeeData.txt");
            Scanner data = new Scanner(fromEmployee);
                        Employee employee = new Employee();

            while (data.hasNextLine())
            {
                employee.readData(data.nextLine());
                payroll.add(employee);
            }

        }
        catch (FileNotFoundException e)
        {
            System.out.println("Error: File Not Found");
        }

    }

}
A: 

Since payroll is an array it would need to be

payroll[index].add(employee);
BrennaSoft
No, then you'd be adding an Employee to a Person.
Michael Myers
@Rpond This is absolutely not true! :S "payroll[index]" returns a "Person" instance... then you'd be calling "add" method on that "Person" instance! What he wants is to add a new "Person" instance to a "Persons" collection.
XpiritO
Agreed, his design is bad. Payroll needs to be List<Employee> payroll and not Person[] payroll;
BrennaSoft
+3  A: 

payroll is an array. You are invoking method on an array. This is not possible.

Petar Minchev
+6  A: 

Instead of using an array use an ArrayList. You'll be much happier.

Arrays cannot be resized once created. All the boilerplate for managing that is done by ArrayList. Using arrays with subclasses in elements has other issues too (around covariance). Probably the only line you need to change is:

private final List<Person> payroll = new ArrayList<Person>();

Lists have an add() method. Arrays don't.

cletus
Just look at me! I use `ArrayLists` all the time and I'm *very happy*! It really works!
Michael Myers
If you don't have a one-track mind, you may need to use Vectors instead to be truly happy.
Syntactic
A: 

If Class B extends A:

A a=new B();

but not:

B b=new A();

John
if Employee extends Person, Employee is a Person. So Person ed = new Employee() is fine. You've got it backwards.
Terry Wilcox
+4  A: 

If for whatever reason you can't use collections. you want to turn:

payroll.add(employee);

in to:

this.add(employee);
DanInDC