2011-02-03 104 views
0

第一篇文章是confusamagin。我的任务是创建一个密码提示程序。需要检查密码是否有至少一个数字和一个字母。此外,密码长度必须介于6 - 10之间。Java代码检查密码的字母和数字?

我的问题是想弄清楚如何查看数字和字母是否存在密码。在检查密码区域,我不知道从哪里开始。我不确定如何查看它是否包含Letter和Digit。我知道怎么做或者通过使用for语句来计数和检查,但是它所做的只是检查而不是看它是否包含所有字母或全部数字。

import java.util.Scanner; 

class Password { 

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

//------ENTER A USERNAME 
     System.out.println("Welcome please enter your username and password."); 
     System.out.print("Username >>"); 
     input.nextLine(); 


//------PASSWORD AUTHENTICATION BEGIN  
     String password = enterPassword(); 
      while (!checkPassword(password)) { 
      System.out.println("Password must be 6 - 10 characters long!"); 
      password = enterPassword(); 
      } 

//------PASSWORD VERIFY 
     String passwordverify = enterPassword(); 
     while (!password.equals(passwordverify)){ 
      System.out.println("ERROR - Passwords DO NOT MATCH Re-Enter Passwords Again"); 
      password = enterPassword(); 

     } 

//------ACCEPT PASSWORD  
     System.out.println("Username and Password Accepted!"); 

     } 


//--ENTER PASSWORD STATEMENT 
    public static String enterPassword(){ 
     String password; 
     Scanner input = new Scanner(System.in); 
     System.out.print("Password >>"); 
     password = input.nextLine(); 
     return password; 
     } 

//--BOOLEAN CHECK PW  
    public static boolean checkPassword(String password){ 
     int length; 
     length = password.length(); 
      if (length < 6 || length > 11){ 
      return false; 
      } 
     for (int i = 0; i < password.length();i++){ 
      if (!Character.isLetter(password.charAt(i))) 
      return false; 
     } 
     return true; 
     } 

} 
+0

为什么你会强迫用户使用字母*和*数字,同时将他们的密码长度限制为10?存储密码散列(就像你应该),并且长度也不重要。 – GolezTrol 2011-02-03 22:42:14

+0

可能的重复[Java代码检查密码的字母和数字?](http://stackoverflow.com/questions/4892142/java-code-check-password-for-letter-and-digit) – trashgod 2011-02-03 22:42:41

回答

1

没有写你的家庭作业....你在正确的轨道上,你必须确保你的信件数量,以及您的号码的数量都至少为1