Skip to content
Advertisement

Java Boolean method return is always false [closed]

I cant figure out why this method only return false, this is what ive got:

  public static boolean something(String word){       
         for (int i = 0; i < word.length(); i++){
             char c = word.charAt(i);
             if((Character.isUpperCase(c)) && (Character.isDigit(c)) && (Character.isLetter(c)) && (Character.isLowerCase(c))
             && (password.length() >= 8 && password.length() <=15));
             return true;
             }
         return false;
 }
}

There is something i am missing, can somebody give me a hint? The code is supposed to check for that string word is between 8 and 15 characters, that it only contains letters and digits, contains at least one upper-case letter, contains at least one lower-case letter, contains at least one digit

Thanks,

Advertisement

Answer

You’re using AND (&&) for your test, which will always be false if you’re testing a character to be Upper Case AND a digit AND a letter AND lower case

If I may, I would recommend that you handle some of these as separate tests. For the requirements you’re specifying my pseudo code would be like this:

  • Set UpperCaseFound, LowerCaseFound, digitFound to not found
  • If password length is < 8 or > 15 then return false
  • for each char
    • if char is UpperCase set UpperCaseFound to true
    • else if char is LowerCase set LowerCaseFound to true
    • else if char is digit set DigitFound to true
    • else return false (as anything not upper, lower or digit is not acceptable)
  • if UpperCase && LowerCase && digitFound is found then return true

Hope it helps

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement