简体   繁体   English

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

[英]Java trouble checking if password string is valid

So I'm trying to get this to take user input as a string and check the password to make sure of these two things: 因此,我试图获取用户输入作为字符串并检查密码,以确保这两件事:

  1. The password is a minimum of 8 characters. 密码至少为8个字符。
  2. The password only contains letters and numbers. 密码仅包含字母和数字。

Now the problem is this: Checking the password for a minimum of 8 characters works, but checking it to make sure it only contains letters and numbers does not work. 现在的问题是:检查密码中至少包含8个字符的方法是可行的,但是检查以确保它仅包含字母和数字是行不通的。 It simply terminates without giving a single message if the minimum amount of numbers/letters are entered. 如果输入了最小数量的数字/字母,它将简单地终止而不给出任何消息。 However if it sees a character that is not a letter or number, it will print out this : 但是,如果看到的字符不是字母或数字,它将显示以下内容

Please enter a password: ### 请输入密码: ###

Password can only contain letters and numbers. 密码只能包含字母和数字。

Password can only contain letters and numbers. 密码只能包含字母和数字。

Password can only contain letters and numbers. 密码只能包含字母和数字。

Password accepted! 密码已接受!

What it should output is this: 它应该输出的是这样的:

Please enter a password: ### 请输入密码: ###

Password can only contain letters and numbers. 密码只能包含字母和数字。

or 要么

Please enter a password: test1234 请输入密码:test1234

Password accepted! 密码已接受!

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

Any help at all with this would be great. 对此的任何帮助都将是巨大的。

You could simplify the code a bit, first start with valid by checking the password.length() ; 您可以稍微简化一下代码,首先通过检查password.length()valid的代码开始; then test each character in the password (stopping if any are invalid). 然后测试密码中的每个字符(如果无效则停止)。 Then finally check if the password was valid before displaying your accepted message. 然后,在显示接受的消息之前,最后检查密码是否有效。 Like, 喜欢,

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

the main error in this check code is the while loop,when you see a wrong character there is no need to continue the loop,simply you do this kind of checks in that way : 此检查代码中的主要错误是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
 }

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

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