Some websites impose certain rules for passwords. Suppose the password rules are as
follows: ¦ A password must have at least eight characters. ¦ A password consists of only letters and digits. Write main () method that prompts the user to enter a password and a static method isValidPassword() that expects a string argument and returns boolean type after validating the password.
Solution:
package p1;
import java.util.Scanner;
class Password
{
String pass;
static boolean isValidPassword(String p)
{
if((p.length()>=8) &&p.matches("[a-zA-Z0-9]*"))
{
return true;
}
else
{ return false; }
}
}
public class Passwordrule
{
public static void main(String[] args)
{
String pas;
Scanner s=new Scanner(System.in);
System.out.println("Enter the password");
pas=s.nextLine();
System.out.println("value in pas is"+pas);
boolean res=Password.isValidPassword(pas);
if(res==true)
{
System.out.println("password is valid");
}
else
{
System.out.println("password is invalid");
}
}
}
No comments:
Post a Comment