I have written an employee class which has a display method and the constructor.
public class employee {
public int empid;
public String name;
public employee(int id, String name){
empid = id;
this.name = name;
}
public void display (){
System.out.println("Employee id: " +empid +"\nEmployee name: "+name);
}
}
I then created three objects of employee and stored them in an array in the main class. I created an if block which will check user guess and print the employee details if he exists or will throw an exception if the data is not present. The if block was enclosed in an enhanced for loop which loops through the array.
public static void main(String[] args) {
// TODO code application logic here
employee priya = new employee (001, "Priya");
employee tamizh = new employee (002, "Tamizh");
employee hari = new employee (003, "hari");
employee[] list = new employee[3];
list[0] = priya;
list[1] = tamizh;
list[2] = hari;
int userGuess = 002;
for (employee l : list){
if (userGuess == l.empid)
{
l.display();
break;
}
else
{
throw new InputMismatchException ("employee doesnot exist");
}
}
}
}
The trouble is that the program throws the exception even if the guess is correct. I tried the int variable empid and then the String variable name, but both the == and .equals didn't work. I searched stackoverflow and the solution suggested was to override the hashcode and equals method in the employee class. I did that.
public class employee {
public int empid;
public String name;
public employee(int id, String name){
empid = id;
this.name = name;
}
public void display (){
System.out.println("Employee id: " +empid +"\nEmployee name: "+name);
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + empid;
result = prime * result + name.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
employee other = (employee) obj;
if (this.empid != other.empid)
return false;
if (this.name.equals(other.name))
return false;
return true;
}
}
Now when I give the input as 001, the code works fine. But for any other input (including the existing empids 002 and 003), the exception is being thrown.
What had I done wrong in the overriding? Also I don't understand the code I had written to override the two methods. Can someone explain the logic and where I went wrong? Thanks.
Edit: Thanks guys. I have realized my mistake in the for loop and I have edited it. It works perfectly now.
int userGuess = 002;
boolean found = false;
for (employee l : list){
if (userGuess == l.empid)
{
l.display();
found = true;
break;
}
}
if(found == false){
try{
throw new InputMismatchException ("employee doesnot exist");
}
catch(InputMismatchException e){
System.out.println("Employee doesnot exist.");
}
}
Thanks a lot guys. Can someone explain what I have done in equals and hashcode methods? I copied the code from an answer and I couldn't find an explanation for it. Thanks again.