验证后需要帮助确认密码

时间:2018-04-19 16:10:21

标签: java string passwords

主程序应该要求密码为8个字符,1个上限,1个下限和1个字符。 1位数。一旦通过要求,它应该要求用户重新输入密码并检查两个密码是否匹配。我需要帮助将该部分添加到我的代码中。目前,代码编译没有问题。

import java.util.Scanner;
public class PassChecker2 {

   public static void main (String [] args) {
       Scanner input = new Scanner(System.in);

       String inputPassword;
       System.out.println(" Please enter your Password:");
       inputPassword = input.next();

       System.out.println(checkerPass(inputPassword));
       System.out.println("");

       //main(args);                     

   }
   public static boolean lenthgCk (String password) {

          if (password.length() > 7) {

             if(checkerPass(password)) {
                return true;
          }
         }
          else {
            System.out.println("Password must be at least 8 characters long.");
            return false;
          }
            return true;
         } 

   public static boolean checkerPass (String password) { 

      boolean hasUpperCase = false;
      boolean hasLowerCase = false;
      boolean hasDigit = false;
      char c;

      for (int i = 0; i < password.length(); i++) {

          c = password.charAt(i);  
          if(Character.isUpperCase(c)) {
             hasUpperCase = true;
          }
          else if(Character.isLowerCase(c)) {
             hasLowerCase = true;
          }
          else if(Character.isDigit(c)) {
             hasDigit = true;
          }
          if(hasUpperCase && hasLowerCase && hasDigit) {
             return true;
          }
          else {     
          System.out.println("Password is invalid must meet all requirements.");
          return false;
          }
   }
          return true;
          }

  }

1 个答案:

答案 0 :(得分:1)

只需输入另一个字符串,就像输入原始密码一样,并检查它们是否相同:

System.out.println(" Please confirm your Password:");
Stirng confirmPassword = input.next();

if (!confirmPassword.equals(inputPassword)) {
    System.out.println("Passwords do not match");
    // And possibly exit the program here?
}