Java – The if condition is ignored

The if condition is ignored… here is a solution to the problem.

The if condition is ignored

This code should check if the password length is

appropriate, but if the password length is greater than or equal to 16, it skips the if condition and does not 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");
    }        
} 

If the password length is greater than or equal to 16, the code should output “Great password length

“, but if the password length is greater than or equal to 16, nothing is output

Solution

if(Password.length()

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

You should organize your conditions in a less confusing way:

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

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

Related Problems and Solutions