Suppose a website impose the following rules for passwords 1
Suppose a website impose the following rules for passwords.
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.
Write a program that prompts the user to enter a password. If the user enters a 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.
Make your program work for handling one password correctly. Then, add the while loop to handle potentially multiple passwords so that the program will stop only when a valid password is typed.
Output:
Ennter the password: abcd
Invaid: at least 8 characters
Ennter the new password: password
Invalid : at least 2 digits.
Ennter the new password: pass 1234
Invalid: only letter and digits
Ennter the new password: pass1234
Valid password!!
start with : [void main]
import java.util.Scanner;
public class CheckPassword1 {
public static void main(String[] args) {
Solution
PasswordChecker.java
import java.util.Scanner;
public class PasswordChecker {
public static void main(String[] args) {
// Declaring variable
String str = null;
boolean digit = false;
int count = 0, flag;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
while (true) {
// Setting flag and count variables to 0
flag = 0;
count = 0;
// setting digit variable to false
digit = false;
// getting the String entered by the user
System.out.print(\"\ Enter String :\");
str = sc.nextLine();
// Checking if the length of the string is less than 8 or not
if (str.length() < 8) {
// Displaying the error message
System.out.println(\"Invaid: at least 8 characters\");
continue;
} else {
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))) {
// Displaying the error message
System.out.println(\"Invalid: only letter and digits\");
flag = 1;
continue;
}
// Checking whether the character is digit or not
else 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)) {
// Display the error message
System.out.println(\"Invalid :at least 2 digits.\");
continue;
}
// If the password is valid then this block will get executed
else if (str.length() >= 8 && digit == true && count >= 2
&& flag == 0) {
// Displaying the valid message
System.out.println(\":: Valid Password ::\");
break;
}
}
}// End of while loop
}
}
_______________________________
Output:
Enter String :abcd
Invaid: at least 8 characters
Enter String :password
Invalid :at least 2 digits.
Enter String :pass 1234
Invalid: only letter and digits
Enter String :pass1234
:: Valid Password ::
________________Thank You


