A weak password is defined as a password with less than eight characters. A medium password is defined as a password having a length of eight or more characters and having either a digit or an “other” character. A strong password is defined as a password having a length of eight or more characters and having both a digit and an “other” character.
How can I return something when ALL 3 conditions MUST be met? My weak pass is the only thing printing right.
public String test() {
//length is user input String which is already converted to into int
//digit and other are booleans,
if (length < 8)
return weak pass;
if ((length <=8) || (length <=8 && digit==true) || (length<=8 && other==true))
return medium pass
if (length <= 8 && digit==true && other==true)
return strong pass;
return null;
}
It should be,
public String test() {
//length is user input String which is already converted to into int
//digit and other are booleans,
if (length < 8)
return weak pass;
if (length >= 8 && digit && other)
return strong pass;
if (length >= 8 && (digit || other))
return medium pass
return null;
}
The shorten version,
public String test() {
if (length < 8) {
return "weak";
} else {
if (digit && other) {
return "strong";
}
if (digit || other) {
return "medium";
}
}
return null;
}
Even more, shorten
public String test() {
return length < 8 ? "weak" : (digit && number) ? "strong" : (digit || number) ? "medium" : null;
}
First translate each written statement into a logic statement:
A weak password is defined as a password with less than eight characters.
length < 8
A medium password is defined as a password having a length of eight or more characters and having either a digit or an “other” character.
length >= 8 && (digit || other)
A strong password is defined as a password having a length of eight or more characters and having both a digit and an “other” character.
length >= 8 && digit && other
Then think about the order you'd want to check these. Check the strongest first, then the medium, then the weak:
public String test() {
if (length >= 8 && digit && other)
return strong;
if (length >= 8 && (digit || other))
return medium;
if (length < 8)
return weak;
return null;
}
Notice how there is one case not covered in the given constraints: A password with length of 8 or more and no digit and no "other" character. For this case null
is returned.
There are a couple of problems.
"weak pass"
and so on.public String test(String password)
{ int length = password.length;
if (length < 8)
etc...
After that you can read through it. In the first if
statement you're testing if length is less than 8 and returning "weak pass"
if it is. When you get to the second if
you already know that length is >= 8 so you don't have to test anymore. You could, if you want to make it extra clear to another developer what you're testing for, but it's not needed and may add clutter that confuses you.
So you wrote the expression ((length <=8) || (length <=8 && digit) || (digit <=8 && other))
there. You can remove the <
's because those are never true. If it's less than 8, you already returned something in your first if. So now you have (length==8 || (length ==8 && digit) || (digit <=8 && other))
I'm not sure if you really meant digit <=8
there or length <=8
. I presume length <= 8
, so then it's (length==8 || (length == 8 && digit) || (length == 8 && other))
If the first part of this condition length==8
is true the result is true. That's how boolean inclusive OR works. So that means the second and third conditions are not needed anymore, because they will only get evaluated if length != 8
, in which case neither one of (length == 8 && digit)
or (length == 8 && other)
is true.
The last if
statement (length <= 8 && digit && other)
requires that length <= 8
. However, if it is you already returned a value in the first or second if statement so the test in the last if
will never be true. You can leave it out.
In summary, what you have is this: if (length < 8) return weak pass; if (length == 8) return medium pass return null;
if (length < 8) return weak pass; if (length == 8) return medium pass return null;
I think you may want to read up on boolean logic. You may find that or
and and
don't mean what you think they do. In common language they are often used interchangeably, but in boolean logic they have very precise meaning.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.