In: Computer Science
Valid or Invalid Password Deliverables
• Complete one document that shows your planning (IPO Chart). Include in it Input, Processing, Output, Variables, Algorithm, Testing Conditions
• Complete the code for your method and test it then upload for grading
Requirements: Write a Java method to check whether a string is a valid password.
Password rules: A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Expected Output:
1. A password must have at least eight characters.
2. A password consists of only letters and digits.
3. A password must contain at least two digits Input a password (You are agreeing to the above Terms and Conditions.): abcd1234 Password is valid: abcd1234
Java Program:
import java.util.Scanner;
public class ValidatePassword
{
public static void main(String[]
args)
{
//To hold the
user's input
String a,
b;
//A Scanner
object for keyboard input.
Scanner a1 = new
Scanner(System.in);
System.out.println("\n 1. A password must have at least eight
characters. \n 2. A password consists of only letters and
digits.");
System.out.print(" 3. A password must contain at least two digits
\n\n Input a password (You are agreeing to the above Terms and
Conditions.): ");
//Prompt the
user to enter the password.
a =
a1.next();
// Validating
for length
if(a.length()
>= 8)
{
int letters = 0, digits = 0, specials = 0;
// Iterating over string
for(int i=0; i<a.length(); i++)
{
//Checking for letters
if(
Character.isLetter(a.charAt(i)) )
{
letters +=
1;
}
//Checking for digits
else if(
Character.isDigit(a.charAt(i)) )
{
digits +=
1;
}
//Checking for special
symbols
else
{
specials
+= 1;
}
}
//Validating combination of characters
if(specials > 0)
{
System.out.println("\n\n
Error!!! Password should be a combination of only Letters, Digits
but not special symbols... \n");
}
//Combination error
else if(digits < 2)
{
System.out.println("\n\n
Error!!! Password should contain at-least 2 digits... \n");
}
else
{
System.out.println("\n\n
Password is Valid: " + a + " \n\n");
}
}
//Length
error
else
{
System.out.println("\n Error!!! Password length
should be minimum eight characters \n");
}
}
}
_______________________________________________________________________________________________________
Sample Run: