"Java Programming Language" Programming Exercises 6.18 (detection code)

6.18 (detection of password) password for some sites have some rules. Write a method to detect whether a string is a valid password.
    Assuming that the password rules are as follows:
    • Passwords must be at least eight characters.
    • The password can only contain letters and numbers.
    • The password must contain at least two numbers.
Write a program that prompts the user for a password, if they meet the rules, Valid Password is displayed, otherwise Invalid Password.
. 1  / **      fileName: passwdRule.java   
 2          function: detecting whether the password entered password rules
 . 3          Create Time: 2019/10/17
 . 4          mail: [email protected]
 . 5    * / 
. 6  
. 7  Import java.util.Scanner;
 . 8  public  class passwdRule {
 . 9      public  static  void main (String [] args) {
 10  
. 11          Scanner iNPUT = new new Scanner (the System.in);
 12 is          of System.out.print ( "enter password:" );
 13 is          String the passwd = iNPUT. Next ();
 14 
15          // password checking 
16          IF (passwdDigit (the passwd)) {
 . 17              IF ((getPasswdNumber (the passwd)> = 2) && ((getPasswdNumber (the passwd) + getPasswdDigitNumber (the passwd)) == passwd.length ())) {
 18 is                  System.out.println ( "Valid Password through" );
 . 19              } the else 
20 is                  System.out.println ( "Invalid Password fail" );
 21 is          } the else {
 22 is              System.out.println ( "Invalid Password fail" );
 23          }
 24      }
 25      
26 is      / **Detecting whether the number of bits length passwd matches the rules in line returns to true * / 
27      public  static  Boolean passwdDigit (String passwd) {
 28          // set the maximum character limit and minimum limit character 
29          Final  int MAX = 1024 ;
 30          Final  int MIN =. 8 ;
 31 is          int passwdLength = passwd.length ();
 32          IF ((passwdLength> = MIN) && (passwdLength < MAX))
 33 is              return  to true ;
 34 is          return  to false ;
 35      }
 36  
37 [      / ** returns the number or the number of letters in passwd , k represents a number 0, 1 represent letters*/
38     public static int getPasswdDigitNumber(String passwd){
39         //字母个数
40         int digitNumber = 0;
41         char digitUpper;
42         for(int i = 0; i< passwd.length(); i++){
43             digitUpper = Character.toUpperCase(passwd.charAt(i));
44             if(digitUpper > 'A' && digitUpper < 'Z')
45                 digitNumber++;
46         }
47         return digitNumber;
48     }
49 
50     /** 返回passwd的数字个数 */
51     public static int getPasswdNumber(String passwd){
52         int number = 0;
53         int arrayNumber;
54         
55         for(int i = 0; i < passwd.length(); i++){    
56             arrayNumber = passwd.charAt(i);
57             if((int)arrayNumber > 47 && (int)arrayNumber < 58)
58                 number++;
59         }
60         return number;
61     }
62 }

 

This code is written one night, delete write, and eventually found charAt (i) This is the key!

Guess you like

Origin www.cnblogs.com/buyiyangdefengcai/p/11696102.html