0

How can i use the selected item from combo box in if statement (the combo box value is converted to string), the java compiler shows an incompatible type error , saying string cannot be converted to Boolean. Please help :) Thank You in advance.

Code:

private void btnSignInActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String userid = txtUserID.getText();
    String username = txtUserName.getText();
    String usertype = cmboUserType.getSelectedItem().toString();

    DBConnector dbcon = new DBConnector();
    dbcon.connect();


    if(dbcon.isUserExists(userid, username,usertype)){
        if (usertype = "Customer"){
           msg.showMessageDialog(
            this,
            "Login Successful",
            "Login Status",
            1);
        OrganicFoods.Customer cust = new OrganicFoods.Customer();
        cust.setVisible(true); 
        }
        else if ( usertype = "StoreAdmin"){
           msg.showMessageDialog(
            this,
            "Login Successful",
            "Login Status",
            1);

        OrganicFoods.StoreAdmin S1 = new OrganicFoods.StoreAdmin();
        S1.setVisible(true); 
        }
         else if ( usertype = "Collection_Delivery_Officer"){
           msg.showMessageDialog(
            this,
            "Login Successful",
            "Login Status",
            1);

        OrganicFoods.Collection_Delivery_Officer cdo1 = new OrganicFoods.Collection_Delivery_Officer();
        cdo1.setVisible(true); 
        }
    }else{
        msg.showMessageDialog(
            this,
            "Login Failure",
            "Login Status",
            0);

    }
}
Dici
  • 25,226
  • 7
  • 41
  • 82

1 Answers1

1

== compares Object reference

.equals() compares String value

Also, to check conditions in your if/else if statements, you must use the == sign and not the = because the = operator will assign the value to the String and not compare it as you want.

Comparing Strings using the == sign is not the best way to check as it compares Objects and not values. So, For the particular program you must use the code snippet as shown below :

if (usertype.equals("Customer")) {
    //your algorithm
}
burglarhobbit
  • 1,860
  • 2
  • 17
  • 32