It is my first lang, so i am new in that field. Please, help me. I am doing my assignment and I received the mistake in process of compilation:"incompatible types". The first class I have done before and it is works. Employee class I should`t touch anymore.
class Employee{
private String firstName; // First name of employee
private String lastName; // Last name of employee
private String id; // Personal id number of employee
private String emNum; // Employee number
static int counter = 0; // The counter variable will keep track the number of employee
public Employee(String firstName, String lastName, String id)
{
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
emNum = "EMPTY";
counter++;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return lastName;
}
public void setid(String id)
{
this.id = id;
}
public String getid()
{
return id;
}
public void setEmNum(String emNum)
{
this.emNum = emNum;
}
public String getEmNum()
{
return emNum;
}
public static int getNumberOfEmployees()
{
return counter;
}
public String toString()
{
if(emNum == "EMPTY"){
setEmNum("Employee number:No employee number has been assigned yet!");
}
else
return emNum;
return ("\n\tFirst Name: " + getFirstName() + " " +"Last Name: " + getLastName()
+ " " + "\n\tId number: " + getid() + " " + "\n\tEmployee number: " + getEmNum() );
}
}
In the A1Q2 class i have problem. In the loop i wanted to create the list of those employees in ONE array. It shows "cannot find symbol" mistake. Actually, I see that types r different, so it cannot write String type into Employee, but i don`t understand how to fix it.
required: Employee
found: java.lang.String
class TestA1Q2
{
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
System.out.println(list);
System.out.println(Employee.getNumberOfEmployees());
}
// DON`T TOUCH ABOVE PART
static Employee[] listOfEmployees(String[] firstNames, String[] lastNames, String[] idNumbers)
{
Employee[] employee = new Employee[firstNames.length];
for (int i = 0; i < firstNames.length; i++){
employee[i] = firstNames[i] + lastNames[i] + idNumbers[i];
}
return employee;
}
}
You are contacting the strings and trying to store them into the Employee array. You have to create a new employee object using the firstName, lastName and ID and then store the reference in the Employee array.
Employee[] employee = new Employee[firstNames.length];
for (int i = 0; i < firstNames.length; i++){
employee[i] = new Employee(firstNames[i],lastNames[i],idNumbers[i]);
}
return employee;
Post back if that worked.