Write a complete Java program called PasswordChecker that gets a String from the
ID: 3791031 • Letter: W
Question
Write a complete Java program called PasswordChecker that gets a String from the user at the command line and checks whether the String, called inputPassword, conforms to the following password policy. The password must: be 3 characters in length include at least one uppercase character include at least one digit If the password conforms to the policy, output "The provided password is valid." Otherwise, output "The provided password is invalid. Please try again." A loop is not needed for this program.
Explanation / Answer
import java.util.Scanner;
public class PasswordChecker
{
public static void main(String s[])
{
String pwd;
char chars[];
char c;
boolean hasDigit=false, hasCap=false;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter Password ");
pwd=sc.next();
if(pwd.length() < 3)
System.out.println("Password must be 3 characters in length and include at least 1 upper case letter and one digit");
else
{
chars= pwd.toCharArray(); // convert string into character array
for(int i=0; i<pwd.length();i++)
{
c=chars[i];
if(Character.isDigit(c)) // Check if the character is digit
hasDigit=true;
else if(Character.isUpperCase(c)) // // Check if the character is Upper case
hasCap=true;
}
if(hasDigit && hasCap)
System.out.println("The provided password is valid");
else System.out.println("The provided password is invalid! Please try again");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.