-7

I want to know why this code is giving me this problem, keep in mind that it has already worked in an earlier Form of the same project but it refuses to work here.

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); 

else
if (jCheckBox1.isSelected()== false)
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");

And if there is any way for me to calculate jCheckBox values using a different method then I'm very eager to learn. My Professor says he knows almost everything about java netbeans and stuff but so far he's not much of a help.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
user3738449
  • 25
  • 1
  • 3

3 Answers3

10

Use curly brackets (braces) and indenting to avoid this sort of trivial mistake.

The code

if (jCheckBox6.isSelected()== true)
jCheckBox6.equals(56);
new Form6().setVisible(true); 

else
if (jCheckBox1.isSelected()== false)
jCheckBox1.equals(0);

is equivalent to

if (jCheckBox6.isSelected()== true) {
    jCheckBox6.equals(56);
}
new Form6().setVisible(true);  // <-- WHAT???

else if (jCheckBox1.isSelected()== false) { // <-- WHERE IS MY IF?
    jCheckBox1.equals(0);
}

While the above explains the syntax error, the resulting program will still be nonsense - this is both due to a nonsensical "else" pairing and because all the chekbox.equals(string) statements don't do (or mean) anything. However, further speculation without a problem description does not seem prudent.

user2864740
  • 60,010
  • 15
  • 145
  • 220
1

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();
}
EtherDragon
  • 2,679
  • 1
  • 18
  • 24
0

I don't see how this could have worked in a previous project.

This else statement is by itself (no corresponding if statement):

else
            if (jCheckBox1.isSelected()== false)

And what is the point of this? You are not assigning this new object to a reference:

new Form6().setVisible(true); 
Edwin Torres
  • 2,774
  • 1
  • 13
  • 15