So for the below question, I tried to search online but I couldn't find the answer to it. I am working in Java language.
So I currently have a class, lets say:
public Employee(String emp_id, String location, String name)
{
this.emp_id = emp_id;
this.location = location;
this.name = name;
}
I have created multiple objects of Employee, and I have saved it in an arrayList. Now, I the user will ask which employees are located in New York, or they can ask which employees are named John.
So they can enter location New York. I need to read in the user's request, first identify what they are trying to search, and then see if there are any matching Employees in the array.
I have read in the command, and saved it in an array of strings called Search. The first index holds the name of the field/property of the object, and the second index will hold what the user actually wants to check.
String[] search = new String[] { "location", "New York" }
I was thinking for doing this:
for(Employee e: empList)
if(e.search[0].equals(search[1]))
System.out.println(e)
However, I am not able to do this, since search[0]
is not a property name for the Employee
object. I am getting this error: error: cannot find symbol.
Is there a way for me to access the object property without the actual name, meaning the name is saved in another String
variable?
Please let me know. Appreciate your help.
Thank you.