密码校验错误

Password Check error

这个会很快,我必须制作一个程序来检查密码是否有效。它必须至少为 8 个字符,具有 1 个小写字符、1 个大写字符和至少 1 个特殊字符才有效。否则,密码将无效。一切似乎都很好,但它似乎永远不会被视为有效,即使它是有效的。我觉得它与角色位置或其他东西有关,但我无法确定它到底是什么。 编辑:更新为包括正则表达式 代码如下:

/* Class:        CS1301
* Section:       9:30
* Term:          Fall 2015
* Name:          Matthew Woolridge
* Instructor:    Mr. Robert Thorsen
* Assignment:    Assignment 6
* Program:       3
* ProgramName:   PasswordTest
* Purpose:       The program prompts the user to input a password and says if it is valid or invalid
* Operation:     The information is statically instantiated in the code and
*                the data is output to the screen.
* Input(s):      The input the password
* Output(s):     The output will be valid or invalid
* Methodology:   The program will use loops to determine if valid or invalid
*
*/

import java.lang.*;
import java.util.*;
public class PasswordTest
{

   public static void main (String[] args)
   {

   /******************************************************************************
   *                          Declarations Section                               *
   ******************************************************************************/
   /****************************CONSTANTS********************************/

      String pass;
      int i;
      boolean valid=false;
      Scanner scan = new Scanner(System.in); //Initializes the scanner

      //"^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$" //RegexChecker

   /******************************************************************************
   *                             Inputs Section                                  *
   ******************************************************************************/
      System.out.print("Please input a password: ");
      pass = scan.nextLine();

   /****************************variables********************************/
    //*********************Using while loop so in processing*********************//

   /******************************************************************************
   *                             Processing Section                            *
   ******************************************************************************/

      for (i = 0; i<pass.length(); i++)
      {
         if(pass.matches("^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$")){
            valid=true;
         }
      }
      if (valid==true){
         System.out.print("Entered Password: " + pass);
         System.out.print("\nThe pass is:      Valid!");
      }
      else{
         System.out.print("Entered Password: " + pass);
         System.out.print("\nThe pass is:      Invalid!");
      }

   /******************************************************************************
    *                              Outputs Section                                *
    ******************************************************************************/
    //*********************The outputs are in the processing*****************//
   } //Ends string
} //Ends program

你的处理部分有一个循环,它只进行一次迭代,然后在最后一个 if-else 语句的 else 块中发生中断。尝试将其移出循环,以检查整个密码,而不仅仅是密码的第一个字母。

还有一个,不需要每次循环都检查密码长度,一次就可以。

for (i = 0; i<pass.length(); i++)
{
     verify = pass.charAt(i);
     if (pass.contains(special))
     {
        specialCheck=true;
     }
     if (Character.isUpperCase(verify)){
        upCheck = true;
     }
     if (Character.isLowerCase(verify)){
        lowCheck=true;
     }
     if (Character.isDigit(verify)){
        digitCheck = true;
     }

     if(upCheck==true && lowCheck==true && digitCheck==true){
        valid = true;
        break;
     }
}
if(valid == true && pass.length()>=8) {
    System.out.print("Entered Password: " + pass);
    System.out.print("\nThe pass is:        Valid!");  
} else {
    System.out.print("Entered Password: " + pass);
    System.out.print("\nThe pass is:        Invalid!");
}

使用正则表达式可以解决您的问题

要成为有效密码的参数是

  • 至少一个小写字母
  • 至少一个大写字母
  • 至少一个特殊符号(/&%^$#@!~+_)
  • 长度至少为 8 个字符

这个正则表达式就可以了

^(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$

例子

public class RegexDemo {

public static void main(String [] args)
{
    String text1 ="ghhthyuj";
    String text2 ="G$hthyu5";
    System.out.println(text1.matches("^(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$"));
    System.out.println(text2.matches("^(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$"));
}

}

输出:text1 = false text2 = true

简短说明:

^                  // the start of the string
(?=.*[a-z])        // use positive look ahead to see if at least one lower case letter exists
(?=.*[A-Z])        // use positive look ahead to see if at least one upper case letter exists
(?=.*[0-9])        // use positive look ahead to see if at least one digit exists
(?=.{8,})           // use positive look ahead to see if length of string is at least 8 charachters
(?=.*[*/&%^*$#@!~+_]) // use positive look ahead to see if at least one special character exists
.+                 // gobble up the entire string
$                  // the end of the string