0

Edit: This is probably very bad code in the PasswordVerifier.Java

I'm doing a password verifier that checks if the entered password is at least 6 characters long, has an Uppercase letter, lowercase letter, and a digit.

I think I have the logistics of it somewhat correct, but for some reason my program isn't going to the next prompt. It asks me for my password and then hangs up and doesn't tell me whether my password is valid or not. I'm thinking that my for loops are correct so I don't know what my issue is.

PasswordVerifier.Java

import java.util.*;

public class PasswordVerifier{

    //field
    private static int MIN_PASSWORD_LENGTH = 6;

    //methods
    public static boolean isValid(String str){

        boolean valid = false;

        PasswordVerifier pass = new PasswordVerifier();

        if(pass.hasUpperCase(str)|| pass.hasLowerCase(str) || pass.hasDigit(str)){
            valid = true;
        }

        if (str.length() < 6){
            valid = false;
        }

        return valid;

    }

    //UpperCase Boolean check
    private boolean hasUpperCase(String str){

        boolean valid = false;

        int i = 0;

        while (i < str.length()){
            if (Character.isUpperCase(str.charAt(i)))
            valid = true;
        }   
        i++;

        return valid;
    }

    //Lowercase Boolean Check
    private boolean hasLowerCase(String str){

        boolean valid = false;

        int i = 0;

        while (i < str.length()){
            if (Character.isLowerCase(str.charAt(i)))
            valid = true;
        }   
        i++;

        return valid;
    }

    //Number boolean check
    private boolean hasDigit(String str){

        boolean valid = false;

        int i = 0;

        while (i < str.length()){
            if ((Character.isDigit(str.charAt(i))))
            valid = true;
        }   
        i++;

        return valid;

    }   

}

PasswordDemo.Java

import javax.swing.JOptionPane;

public class PasswordDemo{

    public static void main(String[] args){

        String input; //To hold the user's input

        input = JOptionPane.showInputDialog("Enter a Password");

            if (PasswordVerifier.isValid(input)){
                JOptionPane.showMessageDialog(null, "Valid Password");
            }
            else{
                JOptionPane.showMessageDialog(null, "invalid Password, try again.");
            }           

    }
}
4

3 回答 3

5

Your while loops never increment i, because you placed i++ just past the end of the loop. The result is an infinite loop and the "hanging" you describe.

E.g. replace

while (i < str.length()){
    if (Character.isUpperCase(str.charAt(i)))
    {
        valid = true;
        // Added break because we found an uppercase letter already.
        break;
    }
    i++;  // Inside the while loop.
}

You'll need to make a similar change to each of your while loops.

Additionally, if you want to enforce all 3 provisions, don't use the logical-OR operator ||, use logical-and, &&:

if (pass.hasUpperCase(str) && pass.hasLowerCase(str) && pass.hasDigit(str)) {
    valid = true;
}

That will make sure that the password has an uppercase letter and it has a lowercase letter and it has a digit.

于 2013-07-02T22:37:28.447 回答
0

我认为您的问题是您在 while 循环之外计算 i++ 。像这样把它放在while中:

private boolean hasUpperCase(String str){

    boolean valid = false;

    int i = 0;

    while (i < str.length()){
        if (Character.isUpperCase(str.charAt(i)))
        valid = true;
        i++;
    }   

    return valid;
}

这是您遇到的每个 while 循环中的问题。

于 2013-07-02T22:39:16.583 回答
0

现在,您正在对密码进行三遍以验证三个字符条件 - 您可以将其压缩为一遍。此外,您最后检查的是密码的长度 - 您应该首先检查它,因为这是最快的验证条件。

public static boolean isValid(String str) {
    if(str.length() < 6) return false;
    int i = 0;
    boolean hasDigit = false;
    boolean hasLower = false;
    boolean hasUpper = false;
    while(i < str.length() && !hasDigit && !hasLower && !hasUpper) {
        if(Character.isDigit(str.charAt(i))) {
            hasDigit = true;
        } else if(Character.isLowerCase(str.charAt(i))) {
            hasLower = true;
        } else if(Character.isUpperCase(str.charAt(i))) {
            hasUpper = true;
        }
        i++;
    }
    return hasDigit && hasUpper && hasLower;
}

在您的情况下,性能提升可能可以忽略不计,但是当您将来可能要处理三个以上的条件和长度远大于六的字符串(或其他)时,请记住这一点。

于 2013-07-02T23:12:35.357 回答