gpt4 book ai didi

java - 使用Java检查密码中的重复字符

转载 作者:行者123 更新时间:2023-11-30 08:15:16 24 4
gpt4 key购买 nike

我是 CIS 的学生,正在上 Java 类(class)(希望成为一名程序员)。无论如何,我有一项任务是创建一个程序来检查密码的有效性。如果密码无效,则打印一条消息。如果密码有效,用户将重新输入密码。如果两者匹配,则打印消息接受。如果不匹配,则打印消息。密码要求是:

  • 长度必须至少为 8 个字符
    1. 必须包含一个数字
    2. 必须包含一个字母
    3. 必须包含一个特殊字符
    4. 不能有 3 个或更多相同的字符
    5. 不能包含空格
    6. 不能以 !或者?

我正在使用 netbeans。

package pass1;

import java.util.Scanner;

public class Pass1 {

//Main method asks for pasword from user and calls upon methods to verify password requirements and password 1 and 2 match
public static void main(String[] args) {
//declare variables
String pass1;
String pass2;
boolean passvalid;
boolean passmatch;


//initialize new instance of scanner
Scanner kb = new Scanner(System.in);
//get password
System.out.println("Please input password. (Must be at least 8 characters, contain a number, letter, and special character");
pass1 = kb.nextLine();
//initialize instance of passisvalid to check password for requirements
passvalid = passisvalid(pass1);
//if pass is valid, allow user to reinsert password
if (passvalid){
System.out.println("Please reinsert password:");
pass2 = kb.nextLine();
//initialize instance of passismatch to check passwords match
passmatch = passismatch(pass1, pass2);
//if passwords do not match, print message
if (!passmatch)
{
System.out.println("Passwords do not match.");
}
//if passwords match, print message
else if (passmatch)
{
System.out.println("Password set.");
}
}
//if password is not valid, print message
else
{
System.out.println("Password is not valid:");
}


}

/*************************************************************************************/

//this method check that user inputted password meets requirements, and returns boolean value
public static boolean passisvalid(String password) {
//declare variables
boolean letter;
boolean digit;
boolean space;
boolean length;
boolean start1;
boolean start2;
boolean valid;
boolean special;
//initialize variables
valid=false;
letter=false;
digit=false;
space=false;
special=false;
length = false;
start1=false;
start2=false;

//initialize count
for(int i=0;i<password.length();i++)
{
char s=password.charAt(i);
//check for letter in password
if(Character.isLetter(s))
{
letter = true;
}
//check for number in password
if(Character.isDigit(s))
{
digit = true;
}
//check for spaces in password
if(Character.isSpaceChar(s))
{
space=true;
}
//check for special characters in password
if(!Character.isDigit(s) && !Character.isLetter(s))
{
special=true;
}
}
//check password length
if (password.length() > 8)
{
length=true;
}
//check password start with ? or !
else if (password.startsWith("?"))
{
start1=true;
}
else if (password.startsWith("!"))
{
start1=true;
}
//requirements of password for boolean true
if(letter && digit && special && length)
{
valid = true;
}
//return boolean false if detect start with ? or !, or spaces in password
if(start1||start2||space)
{
valid = false;
}

return valid;

}

/**********************************************************************************/

//this method checks that both user entered passwords match
public static boolean passismatch(String password1, String password2){
//declare variables
boolean match;
//initialize variables
match=false;

//compare password strings
if (password1.equals(password2))
{
match= true;
}

return match;

}

}

所以除了确保没有连续的重复字符外,我已经能够计算出所有的数字。我真的不知道从哪里开始。我在几个论坛上搜索了几个小时,试图找到一个解决方案并将它们放在一起,但没有看到一个例子,或者只是我一生都无法弄清楚的东西。任何帮助将不胜感激。

最佳答案

可以获取密码到字符串数组,比较每个字符串和后面两个字符串是否相同。

public static boolean hasConsecutiveCharacters(String pwd){
String[] letter = pwd.split(""); // here you get each letter in to a string array

for(int i=0; i<letter.length-2; i++){
if(letter[i].equals(letter[i+1]) && letter[i+1].equals(letter[i+2])){
return true; //return true as it has 3 consecutive same character
}
}
return false; //If you reach here that means there are no 3 consecutive characters therefore return false.
}

关于java - 使用Java检查密码中的重复字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28956272/

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