简单的JAVA:密码验证程序问题

时间:2009-11-13 15:40:45

标签: java string passwords

我有一个简单的问题:

xyz corporation的密码应该是6个字符长,由字母和数字组合而成。编写程序片段以读取字符串并打印出一条消息,指出输入的字符串是否会被视为有效密码。

我需要帮助才能完成此代码。我有这个伪代码,我无法使用Java代码:

print "enter new password"
input newPassword
digitCounter =0
letterCounter = 0
for I = 0 to newPassword.length() by 1
    c = newPassword.charAt(i)
    if c is a digit
        increment digitCounter
    else if c is a letter
        increment letterCounter
    endif
endFor
if newPassword.length() >= 6 and digitCounter > 0 and letterCounter > 0
    print "the password is valid"
else
    print " password rejected, must be at least 6 characters long and be a mix of letters and digits "
endif

+++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++

到目前为止,我所拥有的只是Java代码:

import java.util.Scanner;

public class Password 
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        String thePassword;

        int len, i, letterCounter = 0, digitCounter = 0;
        char c;

        Len = thePassword.length();

        System.out.print("Enter the password: ");
        thePassword = in.nextLine();

        for (i = 0,i = len, )
        {
            c = in.charAt(1);

            if ()
        }

    }
}

5 个答案:

答案 0 :(得分:2)

查看Character.isDigit()Character.isLetter()以查看字符:

如果您想使用String.charAt()来获取字符串的字符,可以执行for循环,如下所示:

for (int i = 0;i < s.length();i++) {
    char c = s.charAt(i);
    //Check things about c
}

虽然Java 1.5引入了For-Each loop,它将自动循环遍历数组:

for (char c : s.toCharArray()) {
    //Check things about c
}

答案 1 :(得分:2)

impot javax.swing.JOptionPane;
class PasswordDemo{
  public static void main(String[] agrs){
     String pass = "abcdef";
     String right = "Success!";
     String wrong = "Failed to login";
     String input = JOptionPane.showInputDialog("Enter the password to login: ");

     do{
        JOptionPane.showMessageDialog(null,wrong);
        input = JOptionPane.showInputDialog("Enter the password to login: ");
     }while(!input.equals(pass));
     //when login successfully
     JOptionPane.showMessageDialog(null,right);
   }
}

答案 2 :(得分:0)

我会检查正则表达式\ d(任何数字)和正则表达式[a-z](任何字母)都匹配字符串。然后检查长度。

答案 3 :(得分:0)

一些快速提示:

  • 您的伪代码算法不正确。它将正确地验证字符串的长度必须至少为6个字符,但不会使其中包含奇怪字符的密码无效(例如〜%)。根据问题陈述,似乎暗示句子“由字母和数字组合而成”意味着构成。对于此部分,正如其他人所提到的,您可以使用String或Character类的内置方法,例如String.charAt(), Character.isDigit()Character.isLetter()

  • 养成在最近可能的时间(即在使用之前)声明内容的习惯。在您的示例中,您有String thePassword,然后是其他2行,然后您为thePassword分配了一些内容。而是直接将其写为String thePassword = in.nextLine()。这将有助于使代码更简洁,更易于阅读。您的其他声明(char cint len等)也是如此。

  • 如果可以,尝试使用增强型for循环,以避免必须弄清楚长度并确定停止的位置(可能出现错误)。在您的示例中,您的循环可能类似于for (char c : thePassword.toCharArray())。如果你还没有在课堂上讨论这个循环,你就不必使用了,你应该知道简单的for循环是如何工作的,但这只是一个建议。在你的代码示例中,你的循环没有意义,所以我建议你阅读循环。

答案 4 :(得分:0)

我假装你只是从命令行参数读入,如果你需要它能够接受多个密码,你可以概括它。以下是我很容易做到的事情:

public class Password {
    public static void main(String[] args) {
        String thePassword = args[0];
        int passLength = thePassword.length();
        boolean hasLetters = false;
        boolean hasDigits = false;
        boolean hasSomethingElse = false;

        for (int i=0; i < passLength; i++) {
            char c = thePassword.charAt(i);
            if(Character.isLetter(c)) hasLetters = true;
            else if(Character.isDigit(c)) hasDigits = true;
            else hasSomethingElse = true;
        }

        if (hasLetters && hasDigits && !hasSomethingElse && (passLength >= 6)) {
            System.out.println("Password is correctly formatted");
        } else {
            System.out.println("Password is not correctly formatted");
        }
    }
}