【问题标题】:Simple JAVA: Password Verifier problem简单的 JAVA:密码验证器问题
【发布时间】:2009-11-13 15:40:44
【问题描述】:

我有一个简单的问题:

xyz 公司的密码应该是 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 ()
        }

    }
}

【问题讨论】:

  • 如果这是作业,请添加“作业”标签。
  • 我看到你的伪代码,还以为是Python……我很困惑,因为标题说的是Java!

标签: java string passwords


【解决方案1】:

查看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
}

【讨论】:

    【解决方案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);
       }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        几个快速提示:

        • 您的伪代码算法不正确。它将正确验证字符串长度必须至少为 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 循环是如何工作的,但这只是一个建议。在您的代码示例中,您的循环没有意义,所以我建议您阅读循环。

        【讨论】:

          【解决方案5】:

          我将假设您只是从命令行参数中读取,如果您需要它能够接受多个密码,您可以对其进行概括。这是我很容易做到的:

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

          【讨论】:

          • 我会先检查密码长度。即在for循环之前。只有当它 >= 6 时,我才会检查字母和数字。对吗?
          • 我考虑过这一点,但我怀疑在这样的简单情况下,它会对函数的计算速度产生很大影响。现在的方式非常干净简洁。我想您可以将 for 循环包含在 if 中(传递长度> = 6)。如果这是一个单独的函数(不只是在 main 内部完成),那么您可以在 if 内部返回 true 或 false,如果跳出它,则始终返回 false。
          猜你喜欢
          • 2023-03-11
          • 1970-01-01
          • 2013-03-04
          • 2016-12-13
          • 2016-04-05
          • 1970-01-01
          • 2017-06-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多