Deescomaster Deescomaster - 4 months ago 17
Java Question

Attaching a new reference to an array?

I have an array that accepts 3

parameters
:
Employees(firstNames, lastNames, idNumbers)
which are string arrays.

String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};


When i call my printing method i get the output:

First name: Fred
Last name: Bond
Id number: R111111
Employee number: No employee number has been assigned yet!

First name: John
Last name: Kates
Id number: A222222
Employee number: No employee number has been assigned yet!


and so on...

As you can see there is no employee number yet assigned. However when i use my
assignEmployeeNum()
method, the Employee number should change to the number in the string array:

String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};


But instead i get this output:

+++After adding the employee number to the list+++

First name: null
Last name: null
Id number: null
Employee number: 1111

First name: null
Last name: null
Id number: null
Employee number: 2222


and so on...

my initial attempt at this problem was just adding another constructor specifically for the employee number but clearly that didn't work. any advice would be appreciated.

main method

public static void main(String[] args)
{
String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};

Employee[] list = new Employee[firstNames.length];
list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array
printEmployeeList(list); //print the list of employees
list = assignEmployeeNum(list,employeeNum); // assign the employee number to the employees


Employee Class

public static class Employee{
private String firstName;
private String lastName;
private String idNumber;
private String employeeNumber;
private int employeeCount;

/**
* Constructor
* @param firstName first name
* @param lastName last name
* @param idNumber id number
*/
public Employee(String firstName, String lastName, String idNumber){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
employeeCount = 0;
}

public Employee(String EmployeeNum){
this.employeeNumber = EmployeeNum;
}
/**
* Accessors here
*/

public String getFirstName(){
return firstName;
}

public String getLastName(){
return lastName;
}

public String getIdNumber(){
return idNumber;
}

public String getEmployeeNumber(){
return employeeNumber;
}

// mutators here

/**
* @param firstName first name
*/
public void setFirstName(String firstName){
this.firstName = firstName;
}

/**
* @param lastName last name
*/
public void setLastName(String lastName){
this.lastName = lastName;
}

/**
* @param idNumber id number
*/
public void setIdNumber(String idNumber){
this.idNumber = idNumber;
}

/**
* @param employeeNumber employee number
*/
public void setEmployeeNumber(String employeeNumber){
this.employeeNumber = "";
}
@Override
public String toString(){
String result = "\nFirst name: " + getFirstName() + "\nLast name: " + getLastName()
+ "\nId number: " + getIdNumber() + "\nEmployee number: ";
if(getEmployeeNumber() == null){
return result + "No employee number has been assigned yet!\n";
}
return result + getEmployeeNumber() + "\n";
}

}


Assign employee method

public static Employee[] assignEmployeeNum(Employee[] list, String[] number){
for(int i = 0; i<list.length; i++){
list[i] = new Employee(number[i]);
}
return list;
}

Answer

You're creating a completely new Employee object within the assignEmployee method, one with null values for every field, rather than using the one that already exists in the array. Solution: don't do that. Instead take the current Employee object held in the array at that position and simply set its employee id in your method.

e.g.,

public static Employee[] assignEmployeeNum(Employee[] list, String[] number){
    for(int i = 0; i<list.length; i++){
       list[i].setEmployeeNumber(number[i]);
    }   
     return list;    
 }
Comments