密码检查错误

时间:2015-10-01 05:22:27

标签: java loops passwords boolean

这个很快,我必须制作一个程序来检查密码是否有效。它必须是至少8个字符,有一个低,一个大写字符,并且至少有一个特殊字符有效。否则,密码将无效。一切似乎都没有问题,但即使它确实有效,也似乎无法结账。我觉得它与角色的位置有什么关系,但我无法确定它究竟是什么。 编辑:更新以包括正则表达式 代码如下:

/* 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

2 个答案:

答案 0 :(得分:2)

在最后一个if-else语句的else块中发生中断之前,在Processing Section中有一个循环,只进行一次迭代。尝试将其移出循环,检查整个密码,而不仅仅是密码的第一个字母。

另外,您不需要每次循环检查密码的长度,只能执行一次。

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!");
}

答案 1 :(得分:2)

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

您的参数是有效密码

  • 至少一个小写
  • 至少一个大写
  • 至少一个特殊符号( /&amp;%^ $#@!〜+ _)
  • 至少长度为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