Ronald Ronald - 4 months ago 14
Java Question

how to add strings from different arrays into one array in java

Hello I'm a bit confused with some coding problem I am trying to solve.

I have a few 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"};


I have to create one array and somehow organize the corresponding pieces of information provided above in the method
Employee[] list = new Employee[firstNames.length];
list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array


I started writing out the method:

public static Employee[] listOfEmployees(String[] firstName, String[]
lastName, String[] idNumber){

}


but not sure how to approach this. also not sure if my parameters are correct.

the end result is supposed to look like this:

Employee #1
first name:Fred Last Name:Bond
Id number:R111111


.
.
.

Employee #2
first name:John Last Name:Kates
Id number:A222222


and so on..

thanks in advance.

EDIT:

Employee class:

public 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;
}
/**
* 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 = "First name: " + getFirstName() + "\nLast name: " + getLastName()
+ "\nId number: " + getIdNumber() + "\nEmployee number: ";
if(getEmployeeNumber() == null){
return result + "No employee number has been assigned yet!";
}
return result + getEmployeeNumber();
}

}

Answer

Please try the following:

private static Employee[] listOfEmployees(String[] firstNames, String[] lastNames, String[] idNumbers){     
   Employee[] list = new Employee[firstNames.length]; 

   for(int i=0; i<list.length; i++){
    list[i]=new Employee(firstNames[i], lastNames[i], idNumbers[i]);
   }    

  return list;
 }

To print the array returned by the above function, you may use:

private static void printEmployees(Employee[] employees){
    for (Employee employee : employees) {
        System.out.println("ID: "+employee.getIdNumber());
        System.out.println("Name : "+employee.getFirstName()+" "+employee.getLastName());
        System.out.println("------------------------------------");
    }
}

And call them by following statement:

printEmployees(listOfEmployees(firstNames,lastNames,idNumbers));

Comments