0

i need to input values to an array inside an array of objects this is the method that will allow the user to enter the values

public static double addRecord(Employee[] employee) {
    Scanner in=new Scanner (System.in);
    for(int i=0; i<employee.length;i++) {
        employee[i]=new Employee();
        System.out.println("Enter the employee's ID and then name (ex: 1587 Ahmad Ashaikh):");
        employee[i].setID(in.nextInt());
        employee[i].setfName(in.next());
        employee[i].setlName(in.next());

        System.out.println("Enter the employee’s (1) BP (2) HA (3) TA (example: 4000 500 300): ");
        for(int j=0; j<3;j++) {
            employee[i].setSalary(in.nextInt(),j);
        }
        break;
    }
    return 0;
}

and that is the class that contains the values

public class Employee {

    private String fname;
    private String lname;
    private int ID;
    private int[] salary;
    private double netSalary;
    private char taxable;

    public Employee() {
        fname="unknown";
        lname="unknown";
        ID=0;
        salary=new int[0];
        netSalary=0;
        taxable='U';
    }

    public String getfName() {
        return fname;
    }

    public String getlName() {
        return lname;
    }

    public int getID() {
        return ID;
    }

    public int getSalary(int index) {
        return salary[index];
    }

    public double getNetSalary() {
        return netSalary;
    }

    public char getTaxable() {
        return taxable;
    }

    public void setfName(String fname) {
        this.fname=fname;
    }

    public void setlName(String lname) {
        this.lname=lname;
    }

    public void setID(int ID) {
        this.ID=ID;
    }

    public void setSalary(int salary,int index) {
        this.salary[index]=salary;
    }

    public void setNetSalary(double netSalary) {
        this.netSalary=netSalary;
    }

    public void setTaxable(char taxable) {
        this.taxable=taxable;
    }
}

i want to enter the salary values .. but it keeps showing me an error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at dd1318398p2.Employee.setSalary(Employee.java:55)
    at dd1318398p2.EmpRecord.addRecord(EmpRecord.java:72)
    at dd1318398p2.EmpRecord.main(EmpRecord.java:36)
Java Result: 1

sorry if my English is bad .. it's not my first language

2
  • Please autoformat your code - I have no idea what's going on and I am under no inclination to try and figure it out. Commented Jul 13, 2014 at 14:08
  • can you also show your main class..i.e. class containing your main method. Commented Jul 13, 2014 at 14:10

2 Answers 2

2

This creates an array without elements - rather useless.

salary=new int[0];

If you don't know the correct number, use a List<Integer> otherwise (e.g.)

salary=new int[3];
Sign up to request clarification or add additional context in comments.

Comments

0

You have kept the kept the salary array capacity as zero. The below statement is giving the issue -: salary=new int[0];

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.