Search code examples
javastringtypescomparemismatch

Java - Compare Strings with equals but says type mismatch string-bool


I just want to compare two strings with each other in line 5 in the posted code but it says: Type mismatch: cannot convert from String to boolean.

but both of them are strings or strings in an array. What is the problem here? This is my Code:

final int ObjektlängeForActionListener = ProjektOBjektlängeGlobal;
comboBox_projekt.addActionListener (new ActionListener () {
  public void actionPerformed(ActionEvent e) {
    for(int key = 0; key < ObjektlängeForActionListener; key++){
      if((String)comboBox_projekt.getSelectedItem().equals(Projektname0JSON[key])){
        if(Verrechenbar0JSON[key] == "1"){
          check_verrechenbar.setSelected(true); 
        }
        if(Verrechenbar0JSON[key] == "0"){
          check_verrechenbar.setSelected(false);
        }
      }
    }
  }
});

Thanks for the help beforehand :)


Solution

  • You cast the result of equals to a String. Remove the cast or cast comboBox_projekt.getSelectedItem() to a String

    if(comboBox_projekt.getSelectedItem().equals(Projektname0JSON[key])){
    

    or

    if(((String)comboBox_projekt.getSelectedItem()).equals(Projektname0JSON[key])){