Project 3: Check Password Problem Description: Suppose a website impose the foll
ID: 3860518 • Letter: P
Question
Project 3: Check Password
Problem Description:
Suppose a website impose the following rules for passwords.
A password must have at least eight characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password. If the user enters an invalid password, the program needs to tell the user why it is invalid and ask the user to enter another password. The program should stop only when the user enters a valid password.
Here is one sample run to check these 4 passwords in order
(My password18 -> pass18 -> password -> Mypassword18):
Enter a new password: My password18
Invalid: Only letters and digits
Enter a new password: pass18
Invalid: Too Few Characters (8 at least)
Enter a new password: password
Invalid: At least two digits
Enter a new password: Mypassword18
Valid password!
Hints:
Start small.
Try to add one rule at a time. Make sure this rule is implemented correctly before adding the next one.
Make your program work for handling one password first. Then, add the while loop to handle potentially multiple passwords so that the program will stop only when a valid password is typed. (Example code of Slide 44 in Chapter 5)
For a password that violates multiple rules, you only need to let the user know the first rule that is violated.
Utilize the Eclipse debugger to make sure your code is running correctly.
Suppose ch takes one character, you can use Character.isLetterOrDigit(ch) to check if ch is letter or digit. Moreover, you can use Character.isDigit(ch) to check if ch is a digit; increase the count of digits by one only when one digit is found.
What to deliver?
Your .java file including:
Your code with other appropriate comments. (Code: 5 points, Comments: 3 points)
One sample run to type the follow four passwords in order (pasted and commented out at top): (2 points)
pw -> password -> itss 3211 -> itss3211
Explanation / Answer
PasswordCheckerUsingMethods.java
import java.util.Scanner;
public class PasswordCheckerUsingMethods {
public static void main(String[] args) {
//Declaring variable
String str = null;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//This loop continue to execute until the user enters a valid password
while(true)
{
//Getting the input entered by the user
System.out.print("Enter a new password :");
str = sc.nextLine();
/* checking whether the password contains letter or digit by calling the method
* if yes,then this block of code get executed and display
* error message and prompt the user to enter again
*/
if (LetterOrDigit(str)) {
//Displaying the error message
System.out.println("Invalid: Only letters and digits");
continue;
/* checking whether the password length is atleast 8 or not by calling the method
* if yes,then this block of code get executed and display
* error message and prompt the user to enter again
*/
}else if(AtLeast8(str))
{
//Displaying the error message
System.out.println("Invalid: Too Few Characters (8 at least)");
continue;
}
/* checking whether the password contains atleast 2 digits by calling the method
* if yes,then this block of code get executed and display
* error message and prompt the user to enter again
*/
else if(AtLeast2Digits(str))
{
// Display the error message
System.out.println("Invalid: At least two digits");
continue;
}
else
System.out.println(":: Valid Password ::");
break;
}
}
/* This method will check whether the password contains
* atleast 2 digits or not
* Params : String
* Return : boolean
*/
private static boolean AtLeast2Digits(String str) {
boolean digit=false;
int count=0;
for(int i=0;i<str.length();i++)
{
if (Character.isDigit(str.charAt(i))) {
digit = true;
// Counting the number of digits
count++;
}
}
/*
* if the string does'nt contain digit or if the count of digits
* is less than 2 then if block will get executed
*/
if (digit == false || (digit == true) && (count < 2)) {
return true;
}
else
return false;
}
/* This method will check whether the password length
* is atleast 8 or not
* Params : String
* Return : boolean
*/
private static boolean AtLeast8(String str) {
if(str.length()<8)
return true;
else
return false;
}
/* This method will check whether the password contains
* only letters or digits or not
* Params : String
* Return : boolean
*/
private static boolean LetterOrDigit(String str) {
int flag=0;
boolean bool = false;
for (int i = 0; i < str.length(); i++) {
// Checking whether the password contains any other thing
// other than digit or character
if (!Character.isDigit(str.charAt(i)) && !Character.isUpperCase(str.charAt(i)) && !Character.isLowerCase(str.charAt(i))) {
flag=1;
}
}
if(flag==1)
return true;
else
return false;
}
}
_____________________________
output:
Enter a new password :My password18
Invalid: Only letters and digits
Enter a new password :pass18
Invalid: Too Few Characters (8 at least)
Enter a new password :password
Invalid: At least two digits
Enter a new password :Mypassword18
:: Valid Password ::
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.