pinkRobot435
pinkRobot435

Reputation: 43

If-statement not functioning properly in NetBeans

I got this code down below:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    Integer intNumber = (Integer)jComboBox1.getSelectedIndex();
    String text = null;

    if (intNumber <= 3) {
        text = "Less than or equal to three";
    } else if (intNumber > 3) {
        text = "Bigger than three";
    }
    jLabel1.setText(text);
}                                        

But if I run this code in Netbeans and choose 4 (which is bigger than 3) in the Combobox, the jLabel1 prints out "Less than or equal to three", even though it obviously is bigger. Could someone explain why?

Upvotes: 1

Views: 628

Answers (1)

tenten
tenten

Reputation: 1276

Try this code. You used getSelectedIndex() instead of getSelectedItem().

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    Integer intNumber = (Integer)jComboBox1.getSelectedItem();
    String text = null;

    if (intNumber <= 3) {
        text = "Less than or equal to three";
    } else if (intNumber > 3) {
        text = "Bigger than three";
    }
    jLabel1.setText(text);
} 

Upvotes: 1

Related Questions