密码输入程序

时间:2014-07-22 16:13:03

标签: java

我正在尝试创建我的作业所需的程序,这是教授的问题:

"编写一个从输入对话框接受用户密码的应用程序。当输入的密码少于六个字符,超过10个字符,或者不包含至少一个字母和一个数字时,再次提示用户。当用户的条目满足所有密码要求时,提示用户重新输入密码,并且在第二个密码与第一个密码匹配之前不要让用户继续。"

一切正常,直到它试图比较输入的两个密码...我不知道最新情况以及为什么它不能正确比较。

这是我的代码:

import javax.swing.*;

public class Password {

public static void main(String[] args) {
    //
    String pInput = "";
    String pInput2 = "";
    int i = 0;
    //do-while loop to obtain password from input and verify input meets requirements in authenticate method.
    do {
        pInput = JOptionPane.showInputDialog(null, "Please enter your password.\n"
                                                    + "Your password must have 6-10 characters\n"
                                                    + "Your password must contain at least one letter and one digit");
    }
    while (authenticate(pInput) == false);

    do {
        pInput2 = JOptionPane.showInputDialog(null, "Please re-enter your password: \n");

        }
    while (compare(pInput, pInput2) == false);

    if (compare(pInput, pInput2) == true) {
        //if input is validated and returns true- finish program
        JOptionPane.showMessageDialog(null, "Your password was successfully entered.");
    }

}   

private static boolean compare(String pass1, String pass2) {
    // TODO Auto-generated method stub
    char[] passArray1 = pass1.toCharArray();
    char[] passArray2 = pass2.toCharArray();
    boolean passCompare = false;
    int x = 0;

    for(int i = 0; i >= pass1.length(); i++) {

        if (passArray1[i] == passArray2[i]) {
            x++;
        }
    }

    if (x == pass1.length()) {
        passCompare = true;
    }
    else passCompare = false;

    if (passCompare = true)
        return true;

    else
        return false;
}

private static boolean authenticate(String password)
{
    // if password is not six characters long or greater than 10 return false - else continue.
      if (password == null || password.length() < 6 || password.length() > 10) {
            return false;
          }
          boolean containsChar = false;
          boolean containsDigit = false;

          //for loop that passes password to an array which finds if array contains a character and digit.
          for (char c : password.toCharArray()) {
            if (Character.isLetter(c)) {
              containsChar = true;
            } else if (Character.isDigit(c)) {
              containsDigit = true;
            }
            if (containsChar && containsDigit) {
              return true;
            }
          }
          return false;
        }
}

3 个答案:

答案 0 :(得分:0)

您可以使用equals()而不是比较功能来简化它。

Example : pInput.equals(pInput2);

答案 1 :(得分:0)

正如另一张海报所说,.equals()更简单并且返回相同的结果,并且它不会不必要地繁琐地使用char数组。

此外,比较函数中的for循环应为&lt;,not&gt; =。否则它将返回异常。

答案 2 :(得分:0)

您的compare()方法存在逻辑错误。

当你使用一个=时,你正在做作业,而不是比较:

if (passCompare = true) // assigns true to passCompare and always evaluates true
    return true;
else
    return false;

更重要的是,不要重新发明轮子。 String类已经有一个非常好的方法可以检查两个String是否相等:equals()方法。

而不是:

do {
    pInput2 = JOptionPane.showInputDialog(null, "Please re-enter your password: \n");

    }
while (compare(pInput, pInput2) == false);

if (compare(pInput, pInput2) == true) {
    //if input is validated and returns true- finish program
    JOptionPane.showMessageDialog(null, "Your password was successfully entered.");
}

......你可以这样做:

    do {
        pInput2 = JOptionPane.showInputDialog(null, "Please re-enter your password: \n");
    } while (!pInput.equals(pInput2));

    if (pInput.equals(pInput2)) {
        //if input is validated and returns true- finish program
        JOptionPane.showMessageDialog(null, "Your password was successfully entered.");
    }

这样可以让您完全取消compare()方法,从而避免在其中犯错误。