2016-03-18 27 views
-1

我必須使用JOptionPane來獲取名稱,社會安全號碼,用戶名和密碼。我有名字和社會保障,但我需要另外兩個。密碼讓我很困惑。它說:如何在JOptionPane中尋求具有獨特複雜性和異常的密碼?

密碼長度不得少於8個字符。它們區分大小寫。我們的複雜性 測試是一個密碼應至少包含一個數字/數字,除第一個 字母之外的一個大寫字母(如果唯一的大寫字母是第一個字母,不通過複雜性測試),一個特殊的 字符(非字母;非數字)。空間是不允許的。

我不知所措。這裏是我的代碼的其餘部分至今:http://pastebin.com/3UGviCzG

+1

請你可以把代碼中的問題本身,而不是鏈接。 –

+0

它說它太長了 – Josh

+1

然後閱讀。 http://stackoverflow.com/help/mcve –

回答

1

How to ask for a password with unique complexities and exceptions in JOptionPane?

無論您是JOptionPane的接受輸入不是一個問題在這裏。即使您從Util.Scanner獲得輸入,密碼檢查的邏輯也是相同的。

如果你不知道從哪裏開始,你總是可以將問題分解成更小的部分。例如。爲每個特定的檢查創建一個輔助方法。

〔實施例:

public static boolean isCorrectLength(String pw){ 
    //your implementation 
} 

public static boolean hasSpecialChar(String pw){ 
    //your implementation  
} 

public static boolean isAlphaNumeric(String pw){ 
    //your implementation  
} 

//And so on.. 

後你解決以上所有子問題。使用它們在你的方法用於驗證口令:

public static boolean isValidPassword(String pw){ 
    return (isCorrectLength(pw) && hasSpecialChar(pw) && isAlphaNumeric(pw)); 
} 

當然,也可以建立只有一個方法。但是,如果你「陷入困境」而不知道該怎麼做。我相信把你的大問題分解成更小的部分確實有幫助。

+0

@Josh你知道現在應該從哪裏開始? – user3437460

+0

http://pastebin.com/Bzi1PJLQ這就是我想出來的,但有編譯錯誤。 – Josh

+0

Program.java:84:錯誤:無法訪問聲明 字符串密碼= JOptionPane的 ^ Program.java:93:錯誤:缺少return語句 } – Josh

1

首先將問題分解爲可管理的塊。

Password should be at least 8 characters in length.

嗯,這很容易......

if (password.length > 7) { 
    ... 

should contain at least one number/digit

Character.isDigit(char) 

one capital letter other than the first letter (if the only capital letter is the first letter, that doesn’t pass the complexity test)

Character.isUpperCase(char) 

你需要做其他檢查時大寫字符數1以確定它是否是第一個字符...

if (!Character.isUpperCase(password[0])... 

one special character (non-letter; non-digit)

Character.isLetterOrDigit(char) 

Spaces are not allowed.

Character.isWhitespace(char) 

您需要驗證這一點,因爲這包括製表符,新線路和一堆其他的空格字符

現在,你只需要把邏輯放在一起。

大部分工作的計數字符和檢查,看看,如果你有正確的數字或​​不

現在,因爲我迂腐,我先來定義核心契約的接口.. 。

public interface PasswordValidator { 
    public boolean isValid(char[] password); 
} 

,然後生成一個或多個實現...

public class ComplexPasswordValidator implements PasswordValidator { 

    @Override 
    public boolean isValid(char[] password) { 
     boolean isValid = false; 
     // At least 8 characters long 
     if (password.length > 7) { 
      int digitCount = 0; 
      int capitalCount = 0; 
      int nonAlphaDigitCount = 0; 
      int whiteSpace = 0; 
      for (char c : password) { 
       if (Character.isDigit(c)) { 
        digitCount++; 
       } else if (Character.isUpperCase(c)) { 
        capitalCount++; 
       } else if (Character.isWhitespace(c)) { 
        // This includes tabs, new lines etc... 
        whiteSpace++; 
       } else if (!Character.isLetterOrDigit(c)) { 
        nonAlphaDigitCount++; 
       } 
      } 
      // Must have a captial character which is not the first character 
      if (capitalCount > 1 || (capitalCount == 1 && !Character.isUpperCase(password[0]))) { 
       // Must have a digit, 1 non alpha/digit character and no whitespace 
       if (digitCount > 0 && nonAlphaDigitCount == 1 && whiteSpace == 0) { 
        isValid = true; 
       } 
      } 
     } 
     return isValid; 
    } 

} 

現在,這些不拋出任何異常,所以唯一的反饋,你會得到我s truefalse。如果您需要添加例外情況,那麼您將不會受到影響。

然後,你可以簡單地使用它像...

PasswordValidator pv = new ComplexPasswordValidator(); 
System.out.println(pv.isValid(new char[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'T', 'e', 's', 't', '_', 'I', 'A', 'm', 'G', 'r', '8'})); 
System.out.println(pv.isValid(new char[]{'B', 'a', 'd', '_', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd', '8'})); 
System.out.println(pv.isValid(new char[]{'B', 'a', 'd', ' ', 'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '8'})); 

你可以設置的歷時的PasswordValidator實例和方法的順序簡單地傳遞你想

The compiling issue is from the password field....?

任何驗證問題#1 ...

return聲明之後放置代碼,這會使代碼不可用

public static boolean checkSSN(String social) { 

    //... 
    return valids; 

    //Code to check password 
    String password = JOptionPane 
      .showInputDialog("Please insert a valid password "); 
    boolean valid = isValid(password); 
    if (isValid(password)) { 
     System.out.println("Valid Password"); 
    } else { 
     System.out.println("Invalid Password"); 
    } 
} 

問題#2 ...

Failing to define the parameter type... 

public boolean isValid(password) { // What type is password? 
+0

http://pastebin.com/rb0wATkH這就是我到目前爲止,但密碼部分沒有編譯正確,我不認爲我有所有例外 – Josh

+0

@Josh你是否嘗試過我的算法?它是否通過了基本要求? – MadProgrammer

+0

@Josh你看過我們的解決方案嗎?您似乎在尋找某人來清理您的代碼,而不是尋求幫助來實施。 – user3437460

相關問題