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,
CodePudding user response:
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
CodePudding user response:
Assuming that argument is "test1234Cc", your code is as follows.
If 't' is uppercase and lowercase and number and letter then return true.
If 'e' is uppercase and lowercase, numbers, and letters, then return true.
If 's' is uppercase and lowercase, numbers, and letters, then return true.
.....
This is impossible!
And In this case
It's better to use a regular expression.
public static boolean something(String word){
String pattern = "((?=.*C)(?=.*c)(?=.*[0-9])).{8,15}$";
return Pattern.matches(pattern, word);
}