Search code examples
javaswingintegerjlabel

How do I compare integer to JLabel?


OK so I have these 2 labels where if both are them are 0, a button will be disabled. This is what I have done but didn't manage to do it, please guide me thanks a lot!

int quantity = 0;
int sum = 0;

I initialize them as 0, after that going through some IF loops and working well, and there's one event which is sort of like clear, I reassign them 0 again which looks like this:

quantity = 0;
sum = 0;

Then now I have 2 Labels which I want to compare to these 2 value, if both are 0, then disable a button, This is what I have done but failed ,the button still remained enabled. Then I realized I'm comparing to string 0 instead of integer 0, how can I compare it with the quantity and sum?? thanks a lot!

 if ("0".equals(jLabel4.getText()) || ("0".equals(jLabel4.getText())));
    {
        jButton2.setEnabled(false);
    }

Solution

  • if you want to compare integers, why compare Strings?

    0 == Integer.parseInt(jLabel4.getText());
    

    also surround it with try-catch block

    boolean equals = false;
    try{
         equals = ( 0 == Integer.parseInt(jLabel4.getText()));
    }catch(NumberFormatException e){
        //equals = false;
    }