dimanche 14 avril 2019

I am keep getting syntax error for not putting ";" in creating ChangeListener object

I am writing if-else statement in stateChanged() method and keep having a syntax error. I do not why I am keep getting an error. I think JSlider s1 is causing the error.

I used the two different anonymous ChangeListener objects for heightSlider and weightSlider, but I want to put into one object which is not anonymous.

    JSlider heightSlider = new JSlider(10,100,50);
    ChangeListener CL = new ChangeListener() {
    @Override
     public void stateChanged(ChangeEvent e) {
     JSlider sl =(JSlider) e.getSource();
     if(sl==weightSlider) {
     weight = sl.getValue();
     status.setText("Your bmi is: "+BMICalculator(weight,height));
     }

     else(s1==heightSlider){//this is where I am keep getting a syntax error
     height = s1.getValue();
     status.setText("Your bmi is: "+BMICalculator(weight,height));
     }
    }       

    }

Because if statement and else statmenet are almost same, I am not sure what is causing the error. I also created JSlider s1 object inside of the method but I think that will not gonna cause the error.

Aucun commentaire:

Enregistrer un commentaire