In: Computer Science
Task Intro: Password JAVA.
Write a method that checks a password. The rules for the password are:
- The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers.
Write a test class that tests the checkPassword method.
Hint: You can (really should) use method from built-in String class:
public boolean matches(String regex)
to check that the current string matches a regular expression. For example, if the variable "password" is the string to be checked, so will the expression.
password.matches("(?:\\D*\\d){3,}.*")
return true if the string contains at least 3 numbers. Regular expression "^ [a-zA-Z0-9] * $" can be used to check that the password contains only numbers and letters.
Let your solution consist of 4 methods:
checkPassword(string password) [only test this method] checkPasswordLength(string password) [checkPassword help method] checkPasswordForAlphanumerics(string password) [checkPassword help method] checkPasswordForDigitCount(string password) [checkPassword help method]
Write a method that checks a password and The rules for the password are:
import java.util.Scanner;
class ValidatePassword {
publics static void main (String [] args) {
String inputPassword;
Scanner input = new Scanner (System.in);
System.out.print("Password: ");
inputPassword= input.next();
System.out.println(PassCheck(inputPassword));
System.out.println("");
main(args);
}
public static String PassCheck (String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x =0; x < Password.length(); x++)
{
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
(Password.charAt(x) >= 97 && Password.charAt(x) <= 122))
{
}
else
{
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58))
{
numCount ++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91))
{
capCount ++;
}
length = (x + 1);
}
if (numCount < 3)
{
result = "Not Enough Numbers in Password!";
}
if (capCount < 2) {
result = "Not Enough Capital Letters in Password!";
}
if (length < 8){
result = "Password is Too Short!";
}
return (result);
}
}
@thank you