[Solved] Else without if error? [closed]


Let’s take a closer look at your code’s conditional structure by correctly separating and using indentations:

I like to add a bit of white-space to clearly know what’s going on.

private void main(void){
    //Indentation shows how program blocks are related to one another
    if (this.condition(parameters) == true)
        // Indentation here shows the following statement is clearly associated with the above condition.
        this.DoSomething(parameters);

Now, with that in mind let’s examine your code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         

    if (jCheckBox1.isSelected()== true)
        jCheckBox1.equals(56);

    if (jCheckBox2.isSelected()== true)
        jCheckBox2.equals(50);

    if (jCheckBox3.isSelected()== true)
        jCheckBox3.equals(56);

    if (jCheckBox4.isSelected()== true)
        jCheckBox4.equals(56);

    if (jCheckBox5.isSelected()== true)
        jCheckBox5.equals(56);

    if (jCheckBox6.isSelected()== true)
        jCheckBox6.equals(56);

    new Form6().setVisible(true); // This statement will always run because it's not associated with a condition.

    else // Oops! Here is an else that is not associated with an if statement! This probably doesn't compile
        if (jCheckBox1.isSelected()== false) // This if is conditional on the else above.
            jCheckBox1.equals(0);

    if (jCheckBox2.isSelected()== false)
        jCheckBox2.equals(0);

    if (jCheckBox3.isSelected()== false)
        jCheckBox3.equals(0);

    if (jCheckBox4.isSelected()== false)
        jCheckBox4.equals(0);

    if (jCheckBox5.isSelected()== false)
        jCheckBox5.equals(0);

    if (jCheckBox6.isSelected()== false)
        jCheckBox6.equals(0);

    JOptionPane.showMessageDialog(this, "Please Choose An Option and Try Again");
} // I assume you mean to close the method

Here is what I see – this code does not use {} blocks to associate code with conditions. This if fine, but realize that only the very next line runs after any if statement if you don’t use the {} blocks.

Example:

if (someCondition)
    this.runSingleLine();

if (someCondition)
    this.runSingleLine();
else
    this.runSomeOtherSingleLine();


if (someCondition)
{
    this.doSomething();
    this.doSomethingElse();
    ...
    this.finishProcedure();
}

solved Else without if error? [closed]