2015-02-11 159 views
-1

這是我的工作,現在的任務...密碼驗證

實施一項計劃,它讀取用戶密碼並驗證它符合以下標準: 是ATLEAST 8個字符長

包含ATLEAST 1個下部字母字符

包含ATLEAST 1上字母字符

包含ATLEAST 1數字位

包含從集合,包括至少一個特殊字符:!@#$%^ & *

不包含「和」字或單詞「結束」

輸出提示用戶輸入一個字符串並在輸出中包含上述要求 。

輸出一個表示有效或無效的字符串。如果無效,說明上面的規則沒有得到滿足。

利用了以下功能:

的indexOf

循環結構

的charAt()

ISDIGIT()

isUpperCase()

isLowerCase()

和所需的任何附加功能

對我來說,棘手的部分是它必須返回所有缺少的東西。就像我輸入了一個表示密碼的密碼,它應該會回來告訴我「你錯過了一個大寫字母,一個數字和一個特殊字符」

我有一個開始,但我很困惑如何讓它回報給我。 這是我迄今爲止

/******************************************** 
    This program will test a password for: 
    8 characters 
    1 upper case 
    1 lower case 
    1 numeric digit 
    1 special character from the set [email protected]#$%^&* 
    and make sure it doesn't contain AND or END 

    If the password complies, it will return a valid answer 
    If not, it will tell the user what they need to do. 
    *********************************************/ 



    import java.util.Scanner; 

    public class YoungAmyProg5 
    { 

     public static void main(String[] args) 
     { 
      Scanner in = new Scanner(System.in); 
      String input; 
      //input password 
      System.out.println("Enter a password that follows    these rules:\n Is at least 8 characters long\n Contains    at least 1 lower case letter\n Contains at least 1 upper   case letter\n Contains at least 1 numeric digit\n      Contains at least 1 special character from the set: !    @#$%^&*\n Does NOT contain the word "and" or the word   "end": ") 
    input= in.nextLine(); 

//Put through string and reply 
if 

    public static boolean isSecurePassword(String password) { 

    int lengthPassword = password.length(); 
    if (lengthPassword >= 8) { 
     return false; 
    } 

    boolean hasUppercase = false; //uppercase 
    boolean hasLowecase = false; //lowercase 
    boolean hasDigit = false; //digit 
    int specialChar = input.indexOf('!', '@', '#', '$', '%', '^', '&', '*'); //special character 
    int word = input.indexOf ('and', 'end'); //and or end 
    for (int i = 0; i < lengthPassword; i++) { 
     char ch = password.charAt(i); 
     if (Character.isUpperCase(ch)) { 
      hasUppercase = true; 
     } 
     if (Character.isLowerCase(ch)) { 
      hasLowercase = true; 
     } 
     if (Character.isDigit(ch)) { 
      hasDigit = true; 
     } 
     if (specialChar>0) { 
      specialChar = true; 
     } 
     if (word>0) { 
      word = true; 
     } 
+0

你應該去'regex'。這將使你的任務變得簡單 – frunkad 2015-02-11 19:11:07

+0

爲什麼不只是讓你的布爾標誌爲全局的,如果你需要它們在你的函數之外呢? – ChrisStillwell 2015-02-11 19:11:45

+0

「我真的很困惑如何讓它回報給我。」你想回報什麼東西?另外,爲什麼在那裏有一個神奇的浮動'if'? – leigero 2015-02-11 19:15:03

回答

0

而不是設置變量,你也可以創建一個列表,存儲缺少的東西和一個布爾值過檢查,如果出現錯誤:

List<String> missing = new ArrayList<String>(); 
boolean verified = true; 

如果裏面你-statements,如果somethíng是錯誤的,請將驗證爲爲false,並將類似'大寫字母'的字符串添加到列表中。

if(<password does NOT contain an upper case letter>) 
    missing.add("an upper case letter"); 
    verified = false; 
} 

畢竟if語句只檢查如果證實是真實的,如果沒有產生一個字符串,從丟失清單,將其添加的所有字符串。(生成的句子)

(我知道這將在一定程度上長碼結束,這是不是太有效率)

+0

你不需要布爾變量 - 只需檢查結束如果missing.size()== 0 – Verim 2015-02-11 21:24:14

+0

我知道 - 這就是爲什麼我寫了最後一句。但我想讓它更容易理解:) – Cyphrags 2015-02-12 18:55:20

0

也許你能想到另一種方式:當你發現一個reqiurement被違反,返回false ;如果最後證明沒有違規行爲,則返回true。