gpt4 book ai didi

java - 密码检查错误

转载 作者:行者123 更新时间:2023-12-01 18:11:09 24 4
gpt4 key购买 nike

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

最佳答案

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

您的有效密码参数是

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

简短说明:

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

关于java - 密码检查错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32879979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com