密码检查器。有关检查密码的方法存在问题

时间:2013-11-24 10:00:00

标签: java

这是一个尝试过的密码检查程序。我想检查密码是否至少8个字符。如果至少1个字符是大写和小写。我还想检查至少一个字符是否是非字母数字。我今天放弃了。我发布它stackoverflow因为我想知道是否有任何人可以帮助我这个。我还不知道如何过度具体化java的技术细节,为什么我的程序工作,我怀疑如果我这样做,我不会在stackoverflow.com上发布它。我对linus torvalds有同样的看法,认为java是一种可怕的语言。然而,我被迫在我的大学完成1个java课程,所以我一直在练习。我还是这门语言的新手。以下是我的计划。

import java.util.Scanner;

public class PasswordChecker {
  public static void main(String[] args) {
    String Password = "";
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a password to be checked");
    Password = input.next();
    PassChkr(Password);
  }

  private static boolean PassChkr(String a) {
    boolean safe = false;
    boolean unsafe = true;

    boolean hasLowercase = !a.equals(a.toUpperCase());
    boolean hasUppercase = !a.equals(a.toLowerCase());

    // Does work
    if (a.length() >= 8) {
        safe = true;
        System.out.println("Your password is at least 8 characters long");
        // Doesn't work..    
    }

    for (int i = 0; i < a.length(); i++) {
      if (hasLowercase == false) {
        safe = true;
        System.out.println("Your password has a least 1 lowercase character");
      }

      if (hasUppercase == false) {
        safe = true;
        System.out.println("Your password has at least 1 uppercase character");
      }
   }

   // for (int i = 0; i < a.charAt(0); i++) {
   //   if (Character.isLetterOrDigit(i) == false) 
   //     safe = true;
   //   System.out.println("Your password has at least 1 non letter or digit");
   //   
   return false;
  }
}

2 个答案:

答案 0 :(得分:3)

我会将问题分解为三个测试(containsUppercontainsLowercontainsNonAlpha)我想按如下方式执行

/**
 * Return true if the input string contains an upper case
 * letter.
 * 
 * @param a
 *          The input string.
 * @return True if a contains an upper case letter.
 */
private static boolean containsUpper(String a) {
  if (a == null) {
    return false;
  }
  for (int i = 0; i < a.length(); i++) {
    if (Character.isUpperCase(a.charAt(i))) {
      return true;
    }
  }
  return false;
}

/**
 * Return true if the input string contains a lower case
 * letter.
 * 
 * @param a
 *          The input string.
 * @return True if a contains a lower case letter.
 */
private static boolean containsLower(String a) {
  if (a == null) {
    return false;
  }
  for (int i = 0; i < a.length(); i++) {
    if (Character.isLowerCase(a.charAt(i))) {
      return true;
    }
  }
  return false;
}

/**
 * Return true if the input string contains a non-alphanumeric 
 * letter.
 * 
 * @param a
 *          The input string.
 * @return True if a contains a lower case letter.
 */
private static boolean containsNonAlpha(String a) {
  if (a == null) {
    return false;
  }
  for (int i = 0; i < a.length(); i++) {
    char c = a.charAt(i);
    if (!Character.isDigit(c) && !Character.isLetter(c)) {
      return true;
    }
  }
  return false;
}

答案 1 :(得分:-1)

我会做类似的事情:

for (int i = 0; i < a.length(); i++) {
  if (Character.isLowerCase(a.charAt(i))) {
    boolean lSafe = true;
  }

  if (Character.isUpperCase(a.charAt(i))) {
    boolean uSafe = true;
  }
}
if(lSafe){
   System.out.println("Your password has a least 1 lowercase character");
}
if(uSafe){
   System.out.println("Your password has a least 1 lowercase character");
}

我会划伤:

boolean safe = false;
boolean unsafe = true;

boolean hasLowercase = !a.equals(a.toUpperCase());
boolean hasUppercase = !a.equals(a.toLowerCase());

不能保证这是最好的方法,但它应该有效。