私は3 parameters
:Employees(firstNames, lastNames, idNumbers)
を受け入れる配列を持っています。これは文字列配列です。配列への新しい参照の追加?
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"};
私は私の印刷メソッドを呼び出すとき、私は出力を得る:
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!
のように...
あなたはまだ割り当てられた従業員番号がありません見ることができるように。しかし、私は私のassignEmployeeNum()
メソッドを使用する場合、従業員の数は、文字列配列内の番号に変更する必要があります。
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};
をしかし、その代わりに、私はこの出力を得る:
+++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
のように...
この問題の私の最初の試みは、従業員番号専用の別のコンストラクタを追加していたに過ぎません。任意のアドバイスをいただければ幸いです。
mainメソッド
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クラス
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";
}
}
割り当て社員方法
public static Employee[] assignEmployeeNum(Employee[] list, String[] number){
for(int i = 0; i<list.length; i++){
list[i] = new Employee(number[i]);
}
return list;
}
配列はパラメータを受け入れることができません。 'Employees(firstNames、lastNames、idNumbers)'は 'Employees'という名前のクラスのコンストラクタ呼び出しのように見えますが、そのようなクラスはありません。 – Andreas