What error it could be

70 views Asked by At

like you could see here in code under that there is no syntax error but each and every line seems right but when this code is being used in my Application the " if " statement not works...... its not matching the string with already existing string array. if i remove that if statements it works fine for all the iteration. (i've checked by putting same entities but it's still not working)

I don't want solution i want to know the reason....??? Thanks

NOTE:

  • s1 ' is a string array (not empty)
  • searchentry ' is a string (not empty)

Code:

public void searchCapability(){
    boolean NotFound=true;
    ///////////////// Linear Folder Search  /////////////
    for(int i=0; i<s1.length; i++){
        if(searchentry==s1[i]){  // code under this if is not working as if is true or false
            l1.setSelectedIndex(i);
            NotFound=false;
        }
    }

    ///////////////////  Linear File search  /////////////
    for(int i2=0; i2<s2.length; i2++){
        // code under this if is not working as if is true or false
        if((searchentry+".xls")==s2[i2]){
            l2.setSelectedIndex(i2); 
            t1.setText(s3);
            NotFound=false;
        }
    }

    if(NotFound==true){
        JOptionPane.showMessageDialog(null, "Entry Not Found  :)", "Error", JOptionPane.INFORMATION_MESSAGE);
    }
}
1

There are 1 answers

1
trogdor On

String comparison using .equals() may give you what you need.

  • .equals() compares values
  • == compares references