Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An if condition gets ignored

This bit of the code is supposed to check if the password length is good, butif the password length is greater than or equals 16, it skips the if condition and doesnt print the sentence.

/* This bit of the coe just checks the length of the password */
if (Password.length() >= 8) {
    if (Password.length() <= 10) {
        System.out.println("Medium length of password");
    }
}
else if (Password.length() < 8) {
    System.out.println("Bruv youre asking to be hacked");
} 
else if (i >= 10) {
    if (Password.length() <= 13) {
        System.out.println("Good password length");
    }
    /* The part that fails */
    else if (Password.length() >= 16) {
        System.out.println("Great password length");
    }        
} 

The code should output "Great password length" if the password length is greater than or equal to 16 but it doesnt output anything if it's greater han or equal ot 16

like image 493
Sim Avatar asked Jan 20 '26 18:01

Sim


1 Answers

if(Password.length() >= 8) and else if(Password.length() < 8) cover all possible password lengths, so the following conditions are never reached.

You should organize your conditions in a less confusing manner:

if (Password.length() < 8) {
    System.out.println("Bruv youre asking to be hacked");
} else if (Password.length() >= 8 && Password.length() <= 10) {
    System.out.println("Medium length of password");
} else if (Password.length() > 10 and Password.length() <= 13) {
    System.out.println("Good password length");
} else if (Password.length() > 13 && Password.length() < 16) {
    ... // you might want to output something for passwords of length between 13 and 16
} else {
    System.out.println("Great password length");
}

or even better

if (Password.length() < 8) {
    System.out.println("Bruv youre asking to be hacked");
} else if (Password.length() <= 10) {
    System.out.println("Medium length of password");
} else if (Password.length() <= 13) {
    System.out.println("Good password length");
} else if (Password.length() < 16) {
    ... // you might want to output something for passwords of length between 13 and 16
} else {
    System.out.println("Great password length");
}
like image 109
Eran Avatar answered Jan 22 '26 08:01

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!