In: Computer Science
Create a Java application called ValidatePassword to validate a user’s password. Write a program that asks for a password, then asks again to confirm it. If the passwords don’t match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid. From that method, call a different method to validate the uppercase, lowercase, and digit requirements for a valid password. Your program should contain at least four methods in addition to the main method.
Follow these rules for a valid password:
The program as per the requirement given in the problem is as follows:
Program:
import java.util.*;
class validatePassword
{
static int same(String str1, String str2)
{
if(str1.equals(str2))
return 1;
else
return 0;
}
static int getLength(String str)
{
int l=str.length();
if(l>=5)
return 1;
else
return 0;
}
static int getUppercase(String str)
{
int count=0;
for(int
i=0;i<str.length();i++)
{
if((int)str.charAt(i)>=65&&(int)str.charAt(i)<=90)
count++;
}
if(count>=1)
return 1;
else
return 0;
}
static int getLowercase(String str)
{
int count=0;
for(int
i=0;i<str.length();i++)
{
if((int)str.charAt(i)>=97&&(int)str.charAt(i)<=122)
count++;
}
if(count>=1)
return 1;
else
return 0;
}
static int getDigit(String str)
{
int count=0;
for(int
i=0;i<str.length();i++)
{
if((int)str.charAt(i)>=48&&(int)str.charAt(i)<=57)
count++;
}
if(count>=1)
return 1;
else
return 0;
}
static int validateP(String str)
{
int length,uppercase,lowercase,digit;
length=getLength(str);
uppercase=getUppercase(str);
lowercase=getLowercase(str);
digit=getDigit(str);
if(length==1&&uppercase==1&&lowercase==1&&digit==1)
return 1;
else
return 0;
}
public static void main(String args[]
)
{
Scanner sc=new
Scanner(System.in);
int valid;
do
{
System.out.print("Enter your password: ");
String
password=sc.nextLine();
System.out.print("Confirm your password: ");
String
confirmPassword=sc.nextLine();
valid=same(password,confirmPassword);
if(valid==1)
{
valid=validateP(password);
if(valid==1)
System.out.println("Validated.");
else
System.out.println("Try
again.");
}
else
{
System.out.println("Try again.\n");
valid=0;
}
}while(valid==0);
}
}
Screenshot of the Code:
Output 1:
Output 2: