May someone tell me the error of this code? My question is display "Found" if the input is in the array, display "Not Found" if the input is not in the array. Why I only can display "Found" for whatever I key in?
String [] deptName = {"Accounting", "Human Resources","Sales"};
//input
String key = JOptionPane.showInputDialog("Enter a department: ");
for(int i=0; i<deptName.length; i++)
{
if(deptName [i] == key);
}
System.out.println("Found");
Edit:
I have amend my code like this, how can I do not let it display 3 times?
String [] deptName = {"Accounting", "Human Resources","Sales"};
//input
String key = JOptionPane.showInputDialog("Enter a department: ");
for(int i=0; i<deptName.length; i++)
{
if(deptName [i].equals(key))
JOptionPane.showMessageDialog(null, "Found");
else
JOptionPane.showMessageDialog(null, "Not Found");
}