I am having problems getting a method from one class to work if I put objects in a set.
So, I have a
public class Employee { private String firstName; private String employeeNumber; public Employee(String employNum) { super(); this.employeeNumber = employNum; }
...
public String getFirstName() { return this.firstName; }
There is a lot of other code that I can send if necessary, but I am not allowed to modify the Employee class.
So, for my code, I need to create a class for Set from Employee , which I did with
public class Records { public Set<Employee> employeeSet = new HashSet<Employee>(); public Records() { } }
Now I need a method that will print the details of all the employees in the set. Here is my attempt so far
public void printEmployeeNames() { for (String employee : employeeSet) { System.out.println(this.employeeSet.getFirstName()); } }
The problem I am facing is that it will not compile as it says
"incompatible types"
and highlights employeeSet in
for (String employee : employeeSet)
Another problem is that it cannot access the getFirstName() method. I tried to isolate the method using
public void printEmployeeNames() { System.out.println(this.employeeSet.getFirstName()); }
It will also not compile as it indicates
"cannot find character - getFirstName () method".
Change Thanks for the help in solving this problem, I changed it to this, and it worked.
public void printEmployees() { for (Employee employee: employeeSet) { System.out.println(employee.getFirstName()); } }