7. Write a Java method to check whether a string is a valid password.
class Main{
static Boolean isPasswordValid(String str){
int len = str.length(), charlen = 0;
boolean hasnum = false, hassymbol = false;
if(len < 12){
return false;
}
for(int i = 0; i < len; i++){
if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z' || str.charAt(i) >= 'a' && str.charAt(i) <= 'z'){
charlen++;
}
else if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
hasnum = true;
}
else if(str.charAt(i) >= '!' && str.charAt(i) <= '@'){
hassymbol = true;
}
}
if(charlen >=8 && hasnum && hassymbol){
return true;
}
return false;
}
public static void main(String[] args){
System.out.println(isPasswordValid("Neha@1406") ? "Password is Valid" : "Invalid Password" );
System.out.println(isPasswordValid("Neha4211") ? "Password is Valid" : "Invalid Password" );
}
}
OUTPUT
Password is Valid
Invalid Password