1

i tried to write my program in different ways but i couldnt figure it out .. the program execute only the code under else even even if the condition is verified can anyone help me please thanks

here is my code :

table.addMouseListener(new MouseAdapter() {

    public void mouseClicked(MouseEvent e) {

        int row = table.getSelectedRow();
        int col = table.getSelectedColumn();
        String path=table.getValueAt(row, col).toString();
        String equipementMOID=txt.getText();

        if(table.isColumnSelected(4) ){
            if(table.getValueAt(row, 3).toString()=="UNIT" ){
                String equipementDate2=comboBox_10.getSelectedItem().toString();
                String xmlFile2=con.XMLSelection(equipementDate2);
                GetNode ne=new GetNode();
                try{
                    String equipementXml2=ne.nodeToString(xmlFile2, equipementMOID);
                    System.out.println(equipementXml2);
                    //GetNodeByPath node=new GetNodeByPath(equipementXml2,path);
                } 
                catch (SAXException | IOException | ParserConfigurationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }    
            else{
                String equipementDate1=comboBox_9.getSelectedItem().toString();
                String xmlFile1=con.XMLSelection(equipementDate1);
                GetNode ne=new GetNode();
                try{
                    String equipementXml1=ne.nodeToString(xmlFile1, equipementMOID);
                    GetNodeByPath node=new GetNodeByPath(equipementXml1,path);
                } 
                catch (SAXException | IOException | ParserConfigurationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }         
        }   
    }
});
Scott
  • 1,863
  • 2
  • 24
  • 43
user771221
  • 127
  • 9

2 Answers2

1

Use the string.equals(String other) function to compare strings, not the == operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. If you actually want to test whether two strings have the same value you should use .equals()

 if(table.isColumnSelected(4) ){
                if(table.getValueAt(row, 3).toString().equals("UNIT") ){
kirti
  • 4,499
  • 4
  • 31
  • 60
0

Try this instead in if condition

table.getValueAt(row, 3).toString().equals("UNIT")

== actually checks the reference(consider it as the location where the object is getting stored) of the String object while equals() checks the value in case of String

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23