验证后需要帮助确认密码

问题描述 投票:-1回答:1

主程序应该要求输入8个字符,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;
          }

  }
java string passwords
1个回答
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?
} 
© www.soinside.com 2019 - 2024. All rights reserved.