Vahx
Vahx

Reputation: 636

Else if condition being ignored?

I'm new to Java and i have to make a small application that will calculate GCD of up to 5 numbers. If the input is nothing before 5 numbers have been entered, the application will calculate it on the already given numbers

Unfortunately my code seems to ignore my else if statement that will make sure it doesn't try to add "" to a int array.

This is the part that i am struggling on, i have already tried contains instead of equals but with no result. Am i writing the !input.. wrong? The code runs correct when i try to add a 0, it will not execute the else if. But if i enter "" to make the application run the first part of the if statement it will go to the else if after its done and try to add "" to the array which of course results in an error. I'm sure its something small i am missing or am unaware of, but i can't seem to figure it out.

}else if(Integer.parseInt(input) != 0 || !input.equals(""));{
            ggdGetallen[count] = Integer.parseInt(input);
            count++;
            txtGetal.selectAll();

}

Full code

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

    String input = txtGetal.getText();

    //Berekenen van het kleinste getal in het array
    if(count > 4 || input.equals("")){
        int kleinsteGetal = ggdGetallen[0];
        for (int getal : ggdGetallen){
            if (getal < kleinsteGetal && getal != 0){
                kleinsteGetal = getal;
            }
        }

       boolean isDividableBy;
        boolean ggdFound = false;
        while(!ggdFound){
             for (int getal : ggdGetallen) {
                if (getal != 0){
                    isDividableBy = (getal % kleinsteGetal == 0);
                    ggdFound = true;
                    if(!isDividableBy){
                        kleinsteGetal--;
                        ggdFound = false;
                        break;
                    }
                }
            }
        }

        lblResultaat.setText(String.format("De grootste gemene deler is %d", kleinsteGetal));


    }else if(Integer.parseInt(input) != 0 || !input.equals(""));{
        ggdGetallen[count] = Integer.parseInt(input);
        count++;
        txtGetal.selectAll();

    }


} 

Upvotes: 0

Views: 528

Answers (1)

SMA
SMA

Reputation: 37023

remove semicolon from your else if.

Upvotes: 5

Related Questions