-3

As shown below I declared an ArrayList of EmployeesRecords and added employees but I don't understand why this is not comparing. I'm new to java, can someone please help

ArrayList<EmployeeRecords> employees = new ArrayList<EmployeeRecords>();

When the remove button is pressed, it should remove the employee if his/her Id number is equal to one entered by the user.

String employeeID = textEmployeeID.getText();
if(employeeID == String.valueOf(employees.get(0).ID)){
    employees.remove(0);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Note that the actual problem here has nothing to do with swing, user-interface or java-8 & everything to do with string, arraylist & string-comparison. By choosing tags more carefully, you attract the right audience! I have edited the post to remove the irrelevant tags and add the relevant ones. – Andrew Thompson Nov 04 '20 at 01:44

1 Answers1

-1

When comparing two String, use var1.equals(var2). In your case,

String employeeID = textEmployeeID.getText();
if (employeeId.equals(String.valueOf(employees.get(0).ID)){
    employees.remove(0);
}
  • I tried this and it didn't work. How do you compare an ArrayList of objects defined by the EmployeeRecords class to employeeID of a type string? – newToJava Nov 04 '20 at 00:28