0

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");
}
1
  • Hi, I've edited your question so that anyone else coming along later can see the whole conversation, not just the latest step. Commented Dec 8, 2011 at 15:41

6 Answers 6

3

"found" was always printed, because it is out of the for loop. and your check (which has problem too) does actually nothing

try

    your For loop {
         if(deptName [i].equals( key)){
          System.out.println("Found");
          break;
}
    }
Sign up to request clarification or add additional context in comments.

Comments

2

If you use == on a Object in java, you only check if the object is the exact same object. To compare the content of an object, you should use equals().

In case of the class String, equals() compares the actual String content.

deptName[i].equals(key);  //returns true if text is the same

Comments

1

Do it like this

for(int i=0;i<deptName.length;i++){
    if(key.equals(deptName[i]){
       JOptionPane.showMessageDialog(null,"Found"); break;
}}
if(i==deptName.length)
   {JOptionPane.showMessageDialog(null,"Not Found");}

Comments

0

You have several issues, first you need to call equals() and must not terminate the if with ';'

for(int i=0; i<deptName.length; i++)  {         
    if(deptName [i].equals( key )) {
       System.out.println("Found"); 
    }
}

would work as intented.

Comments

0
int i=0 ;   
for(; i<deptName.length; i++) { 
    if(key.equals(deptName[i])) break;    
} 
if( i < deptName.length) {
       // found
} else {
      // not found
}

2 Comments

I have amend my code like this, how can I let it do not 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"); }
0

Screw the loop.

JOptionPane.showMessageDialog(null,Arrays.asList(deptName).contains(key) ? "Found" : "Not Found"); 

I understand this could be a programming exercise, so here you go...

for(int i=0;i<deptName.length;i++){
    if(key.equals(deptName[i])
       break;
}

if(i==deptName.length)
   JOptionPane.showMessageDialog(null,"Not Found");
else
   JOptionPane.showMessageDialog(null,"Found at " + i);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.