1import java.util.Scanner;
2
3public class PromptPassword {
4
5 public static void main(String[] args){
6 Scanner sc = new Scanner(System.in);
7 System.out.print("Password rules:\n"
8 + "Password must have at least eight characters\n"
9 + "Password must consist of only letters and digits\n"
10 + "Password must contain at least two digits\n"
11 + "Enter a password:");
12 String pWT = sc.nextLine(); //read the entire line
13 passWordIsValid(pWT.trim()); // to remove leading spaces (if any)
14 }
15 public static void passWordIsValid (String password) {
16 boolean passWordIsValid = true;
17 int noOfDigits =0;
18
19
20 if (password.length() > 8) { // if it's less than 8 chars -> no need to continue
21 for(char c : password.toCharArray()){
22 if(Character.isDigit(c)){ // if it's a digit, increment
23 noOfDigits++;
24 }
25 else if(!Character.isAlphabetic(c)){ // if it's not a digit nor alphapet
26 passWordIsValid = false;
27 break;
28 }
29 }
30 }
31
32 if (passWordIsValid && noOfDigits>=2){
33 System.out.print("Password is valid");
34 }
35 else{
36 System.out.println("Password is invalid");
37 }
38
39 }
40}
41