2014-02-15 109 views
0

我想完成我的編程任務,我必須創建一個具有1個大寫,1個小寫,1個數字和1個特殊字符的「密碼驗證程序」。如果無效,它必須顯示「無效」加上它顯示爲false的規則。我遇到的問題不是錯誤消息,但我總是得到積極的迴應,再加上所有違反的規則,再加上它讀兩次。這裏幫助不大?密碼驗證問題

import java.util.Scanner; 

public class PasswordVerifier { 
    public static void main(String[] args) { 
     System.out.println("Password Verifier"); 
     System.out.println("Enter a password that meets the following rules: "); 
     System.out.println("-Must be at least 8 characters long" + '\n' + 
       "-Must contain at least 1 lower case character" + '\n' + 
       "-Must contain at least 1 upper case character" + '\n' + 
       "-Must contain at least 1 numeric digit" + '\n' + 
       "-Must contain at least 1 special character from the set: [email protected]#$%^&*" + '\n' + 
       "-Must not contain the word 'and' or the word 'end'"); 

     String password; 
     String contains1 = "and"; 
     String contains2 = "end"; 
     String special = "[email protected]#$%^&*"; 

     Scanner stdIn = new Scanner(System.in); 
     boolean digit = false;  //Has at least 1 digit 
     boolean upper = true;  //Has at least 1 upper case letter 
     boolean lower = true;  //Has at least 1 lower case letter 
     boolean hasspecial = true; //Has at least 1 special character 
     boolean length = true;  //Has at least 8 digits 
     boolean endand = true;  //Does not contain end or and 
     boolean valid = false;  //Is the password valid? 


     System.out.println("Enter password: "); 
     password = stdIn.nextLine(); 


     int result; 
     result = password.indexOf(contains1); 
     if (result == -1) { 
      System.out.println(""); 
     } else { 
      System.out.println("Must not contain the word 'and'"); 
     } 

     int result2; 
     result2 = password.indexOf(contains2); 
     if (result2 == -1) { 
      System.out.println(""); 
     } else { 
      System.out.println("Must not contain the word 'end'"); 
     } 

     if (password.length() < 8) { 
      System.out.println("Must be at least 8 characters long."); 
     } else { 
      System.out.print(""); 
     } 
     for (int i = 0; i < password.length(); i++) { 
      if (!(Character.isUpperCase(password.charAt(i)))) ; 
      { 
       upper = false; 
       valid = false; 
       i++; 
      } 
      if (!(Character.isLowerCase(password.charAt(i)))) ; 
      { 
       lower = false; 
       valid = false; 
       i++; 
      } 
      if (!(Character.isDigit(password.charAt(i)))) ; 
      { 
       digit = false; 
       valid = false; 
       i++; 
      } 
      if (!(password.matches(special))) ; 
      { 
       hasspecial = false; 
       valid = false; 
      } 

      if (upper != true) { 
       System.out.println("Must contain an upper case letter."); 
      } 
      if (lower != true) { 
       System.out.println("Must contain a lower case letter."); 
      } 
      if (digit != true) { 
       System.out.println("Must contain a numeric digit."); 
      } 
      if (hasspecial != true) { 
       System.out.println("Must contain a special character."); 
      } 
      if (valid) { 
       System.out.println("Valid."); 
      } else if (valid != true) { 
       System.out.println("Invalid."); 
      } 
     } 
    } 

} 
+2

選擇一個密碼,在紙上執行你的算法,注意每個步驟的每個變量的值。只是一個提示:你發現一個不是大寫字符的事實並不意味着該密碼沒有任何大寫字母。 –

+0

你從來沒有將** true **賦值給** valid **變量。所以你怎麼能得到積極的迴應? – Devavrata

+0

所以我改變了「有效」爲「真實」,但我得到相同的問題... – user3313912

回答

0

有一點。你所有的線路都是錯誤的。

您正在添加';'之後如果。這意味着,如果滿足if條件,則不執行任何操作,則始終執行以下幾行。

另一件事,當你做

if (!(password.matches(special))) { 
    hasspecial = false; 
    valid = false; 
} 

您所要求的密碼,以匹配字符串 「!@#$%^ &(星號)」,它可能不會。所以,既然你假定字符串包含一個特殊字符,hasspecial將永遠是真的。你想寫一些類似password.matches(「。(星號)[!@#$%^ & *]。(星號))(可能會轉義一些字符)

最後,你說的是,如果一封信如果下面的字符符合條件,那麼標記仍然是假的,通常你應該通過假設所有的條件都是假的來處理這類問題,然後設置標記爲true,一旦條件滿足

注:我寫的(星號),而不是*,becasue文本被突出顯示,我不知道怎麼逃生呢:(

+0

謝謝,幫助! – user3313912