简体   繁体   English

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

[英]Checking for repeated characters in password using Java

I'm a student of CIS taking a Java class (hoping to become a programmer). 我是CIS的学生,上了一门Java课(希望成为一名程序员)。 Anyhow, I have an assignment to create a program that checks a password for validity. 无论如何,我分配了一个程序来检查密码的有效性。 If the password is not valid, it is to print a message. 如果密码无效,则将打印一条消息。 If the password is valid, the user is to reinsert the password. 如果密码有效,则用户将重新插入密码。 If they both match, it is to print a message accepting. 如果它们都匹配,则打印一条接受消息。 If they do not match, it is to print a message. 如果它们不匹配,则将打印一条消息。 The password requirements are: 密码要求为:

  • Must be at least 8 characters in length 长度至少必须为8个字符
    1. Must contain one number 必须包含一个数字
    2. Must contain one letter 必须包含一个字母
    3. Must contain one special character 必须包含一个特殊字符
    4. Cannot have 3 or more identical characters 不能包含3个或更多相同的字符
    5. Cannot contain spaces 不能包含空格
    6. Cannot start with ! 无法开始! or ? 要么 ?

I am using netbeans. 我正在使用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;

}

}

So I've been able to figure all except for making sure there are no consecutive repeating characters. 因此,除了确保没有连续的重复字符之外,我已经能够弄清所有内容。 I really don't even know where to begin with this. 我什至不知道从哪里开始。 I've searched for hours on a few forums to try and find a solution and put it all together but without seeing an example or just something I can't figure it out for the life of me. 我在几个论坛上搜索了几个小时,试图找到一个解决方案并将其整合在一起,但是却没有看到一个示例或仅仅是我无法解决的事情。 Any help would be IMMENSELY appreciated. 任何帮助将不胜感激。

You can get password to string array and compare each string with the next two string whether it has the same. 您可以获取字符串数组的密码,并将每个字符串与接下来的两个字符串进行比较(无论是否相同)。

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.
    }
package filehandling;

import java.util.Scanner;

public class PasswordValidation {

    public static void main(String[] args) {

        String enterPassword ;
        String reEnterPassword ;

        boolean passValid ;
        boolean isPassMatch ;

        Scanner password = new Scanner(System.in);
        System.out.println("Enter Your Password:");
        System.out.println("(Must be at least 8 characters,  contain a number, letter, and special character");
        enterPassword = password.nextLine();

        passValid = checkPassIsValid(enterPassword);

        if(passValid) {
            System.out.println("Re-Enter Your Password: ");
            reEnterPassword = password.nextLine();
            isPassMatch = checkPasswordMatching(enterPassword,reEnterPassword);

            if(!isPassMatch)
                System.out.println("Password do Not Match: ");
            else if (isPassMatch)
                System.out.println("Password Set.");

        }
        else
            System.out.println("Password is Not Valid.");

    }

    private static boolean checkPassIsValid(String enterPassword) {

        boolean length = false;
        boolean letter = false;
        boolean digit = false;
        boolean space = false;
        boolean startSpace = false;
        boolean special = false;
        boolean start1 = false;
        boolean start2 = false;
        boolean valid = false;

        for(int i = 0; i < enterPassword.length(); i++) {
            char c = enterPassword.charAt(i);

            if(enterPassword.length() >= 8)
                length = true;
            if(Character.isDigit(c))
                digit = true;
            if(Character.isLetter(c))
                letter = true;
            if(Character.isSpaceChar(c))
                space = true;
            if( (!Character.isDigit(c)) && (!Character.isLetter(c)) )
                special = true;
            if(enterPassword.startsWith(" "))
                startSpace = true;
            else if(enterPassword.startsWith("?"))
                start1 = true;
            else if(enterPassword.startsWith("!"))
                start2 = true;
            if(letter && digit && special && length)
                valid = true;
            if(start1 || start2 || startSpace)
                valid = false;
        }
        return valid;

    }

    private static boolean checkPasswordMatching(String enterPassword,String reEnterPassword) {

        if(enterPassword.equals(reEnterPassword))
            return true;        
        return false;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM