0
//employee class  
public class Employee {


    //properties
    private String name;
    private int age;
    private int salary;


    //constructors
    public Employee(String name,int age, int salary){
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public Employee(Employee instance){
        name = instance.name;
        age = instance.age;
        salary = instance.salary;
    }

    //Methods

    //getter and setter methods
    public  String getName(){
        return name;
    }
    public  int getAge(){
        return age;
    }   

    public  int getSalary(){
        return salary;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setAge(int age){
        this.name = name;
    }

    public void setSalary(int salary){
        this.salary = salary;
    }   

    //toString
    public String toString(){
        String result;
        result = name + ", " + age + ", $" + salary;
        return result;
    }
}


//company management
import java.util.Scanner;
public class CompanyManagement {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Employee[] employeeList;
        employeeList = new Employee[20];
        String name;
        int age;
        int salary;
        int choice;
        int i;

        i =0;

        do{

            System.out.println( "Welcome to the Company Management System. Please enter your choice" + "\n(1)Add Employee" + "\n(2)Delete Employee" + "\n(3)Change Employee Salary" + "\n(4)Exit" + "\nChoice: ");
            choice = scan.nextInt();

            if(choice == 1)
            {   

                System.out.println( "Please enter employee name: ");
                name = scan.nextLine();
                name = scan.nextLine();

                System.out.println( "Please enter employee age: ");
                age = scan.nextInt();

                System.out.println( "Please enter employee salary: ");
                salary = scan.nextInt();

                employeeList[i] =  new Employee(name,age,salary);


                for(int k = 0;k<=i;k++)
                    System.out.println(employeeList[i] );
                i++;

            }   

            if(choice == 2)
            {

            }

            if(choice ==3)
            {

            }


        }while(choice!=4);
    }
}

Why does the below code not assign a new reference each time while the loop is repeating and instead it assigns the last value of all indexes of the array. How can I fix this reference problem?

 employeeList[i] =  new Employee(name,age,salary); 

2 Answers 2

4

Your code works fine. Just instead of:

 System.out.println(employeeList[i]);

put

 System.out.println(employeeList[k]);
Sign up to request clarification or add additional context in comments.

Comments

3
           for(int k = 0;k<=i;k++)
                System.out.println(employeeList[i] );

You are looping k and indexing on i.

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.