Java检查密码字符串是否有效

时间:2017-01-07 20:25:56

标签: java string passwords boolean

所以我试图让用户输入字符串并检查密码以确保这两件事:

  1. 密码至少为8个字符。
  2. 密码只包含字母和数字。
  3. 现在的问题是: 检查密码至少8个字符有效,但检查密码以确保它只包含字母和数字不起作用。如果输入最小数量/字母,它只会在不给出单个消息的情况下终止。 但是,如果它看到的字符不是字母或数字,则会打印出

    请输入密码:###

    密码只能包含字母和数字。

    密码只能包含字母和数字。

    密码只能包含字母和数字。

    密码已被接受!

    输出的内容是:

    请输入密码:###

    密码只能包含字母和数字。

    请输入密码:test1234

    密码已被接受!

        password.java    
        package Password;
        import java.util.Scanner;
    
        public class Password {
    
        public static void main(String[]args)
        {
    
        Scanner input = new Scanner (System.in);
        boolean valid = true;
        System.out.println("Please enter a password:");
        String password = input.nextLine();
        int i = 0;
        //declares i as the counter varible to control the loop and initializes it to 0
    
        if((password.length( ) < 8 )) //check the passwords length and make sure it's a minimum of 8 characters
        {
        System.out.println("Password must have at least 8 characters.");
        valid = false;
        }
        //loops the code below it for the length of i until the password length is reached
        while(i < password.length())
        {
        if ((password.charAt(i)>='a' && password.charAt(i)<='z') ||  (password.charAt(i)>='A' && password.charAt(i)<='Z') ||(password.charAt(i)>='0' && password.charAt(i)<='9'))
        //loop through all the characters in the string entered and make sure they only consist of letters and numbers
           valid = true;
        else
        {
            System.out.println("Password can only contain letters and numbers.");  
            valid = false;
        }
        i++;
        //add an iteration to the loop
        }
    
        if(!valid == true)
        System.out.println("Password accepted!");
        }
        }
    

    任何与此有关的帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

您可以稍微简化一下代码,首先查看valid,然后选择password.length();然后测试密码中的每个字符(如果任何字符无效则停止)。然后在显示接受的消息之前,最后检查密码是否有效。像,

Scanner input = new Scanner(System.in);
System.out.println("Please enter a password:");
String password = input.nextLine();
boolean valid = password.length() >= 8;

if (!valid) {
    System.out.println("Password must have at least 8 characters.");
} else {
    for (char ch : password.toCharArray()) {
        if (!Character.isLetter(ch) && !Character.isDigit(ch)) {
            System.out.println("Password can only contain letters and numbers.");
            valid = false;
            break;
        }
    }
}
if (valid) {
    System.out.println("Password accepted!");           
}

答案 1 :(得分:0)

此检查代码中的主要错误是while循环,当您看到错误的字符时无需继续循环时,只需以这种方式执行此类检查:

String toCheck;       //the string to check some criteria 
boolean valid = true; // we assume that nothing wrong happen till now

for(int i=0;i<toCheck.length();i++){ //loop over the characters
    if(/*condition of wrong case*/){ 
        valid = false;               //mark that something wrong happen
        break;                       //exit the loop no need to continue
    }
 }

 if(valid){
    //nothing wrong happen
 } else {
    //something wrong happen
 }