如何更改此java代码,以便再次询问密码是否无效?

时间:2015-01-26 19:06:39

标签: java

import javax.swing.JOptionPane;

class PasswordCreator
{
        public PasswordCreator()
        {
                super();
        }

        public static void main(String[] args)
        {
                PasswordCreator passwordCreator = new PasswordCreator();
                String userName = JOptionPane.showInputDialog("What is your username?");

                String passWord = JOptionPane.showInputDialog("What is your password?");
                System.out.println("Input : UserName "+userName+" PassWord -> "+passWord);

                passwordCreator.passwordValidation(userName,passWord);
                System.out.println();

        }

        /*
         * Password should be less than 15 and more than 8 characters in length.
         * Password should contain at least one upper case and one lower case alphabet.    
         * Password should contain at least one number. 
         * Password should contain at least one special character.
         */

        public void passwordValidation(String userName, String password)
        {
                boolean valid = true;
                if (password.length() > 15 || password.length() < 8)
                {
                        System.out.println("Password should be less than 15 and more than 8 characters in length.");
                        valid = false;
                }
                if (password.indexOf(userName) > -1)
                {
                        System.out.println("Password Should not be same as user name");
                        valid = false;
                }
                String upperCaseChars = "(.*[A-Z].*)";
                if (!password.matches(upperCaseChars ))
                {
                        System.out.println("Password should contain atleast one upper case alphabet");
                        valid = false;
                }
                String lowerCaseChars = "(.*[a-z].*)";
                if (!password.matches(lowerCaseChars ))
                {
                        System.out.println("Password should contain atleast one lower case alphabet");
                        valid = false;
                }
                String numbers = "(.*[0-9].*)";
                if (!password.matches(numbers ))
                {
                        System.out.println("Password should contain atleast one number.");
                        valid = false;
                }
                String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
                if (!password.matches(specialChars ))
                {
                        System.out.println("Password should contain atleast one special character");
                        valid = false;
                }
                if (valid)
                {
                        System.out.println("Password is valid.");
                }
        }
}

如果密码无效,我希望程序要求输入新密码。我尝试在线搜索,但我没有成功将其实现到我的代码中。谁知道怎么做?

1 个答案:

答案 0 :(得分:5)

首先,您应该更改passwordValidation方法,以便它可以返回一个布尔值,指示密码是否正确:

public boolean passwordValidation(String userName, String password) {
    boolean valid = true;
    /* your code */
    return valid;
}

然后,在您的main方法中,您可以执行以下操作:

boolean valid = false;
while (!valid) {
    String passWord = JOptionPane.showInputDialog("What is your password?");
    valid = passwordCreator.passwordValidation(userName,passWord);
}

顺便说一下,userName的{​​{1}}参数是没用的,因为它从未使用过。