【问题标题】:Password Check error密码检查错误
【发布时间】:2015-12-29 01:25:01
【问题描述】:

这会很快,我必须编写一个程序来检查密码是否有效。它必须至少有 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

【问题讨论】:

    标签: java loops passwords boolean


    【解决方案1】:

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

    您的有效密码参数是

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

    【讨论】:

    • 正则表达式有时可以工作,但它似乎只想使用特定密码而不是任何顺序,只要满足这些要求。例如, G$hthyuj 有效,但如果我输入 fa#4F 它返回无效。请帮助@Nishant123。
    • 您上面提到的密码有效参数不包含数字。所以我制作了一个适合您要求的正则表达式。我已经编辑了我的答案并更改了正则表达式。现在它应该需要一位数才能有效
    • 哦,我不敢相信我没有注意到我自己或者我会把它包括在内。非常感谢! @Nichant123 它就像一个魅力!
    【解决方案2】:

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

    还有一点,你不需要每次循环检查密码的长度,你可以只做一次。

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

    【讨论】:

      猜你喜欢
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多