2016-09-14 48 views
-3

我對這一切都很陌生,所以讓我用它作爲序言,以防萬一這看起來真的很糟糕。的細節是在標題,但它基本上是等於或8個字符,一個上殼體,一個下殼體,和一個符號,既不是字母或數字我想在java中創建一個長度爲8個字符或更多,一個大寫,一個小寫字母和一個符號的密碼檢查器。

 Scanner s = new Scanner(System.in); 
     String outputString = ""; 
     String lowerCaseAlphabet = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"); 
     String upperCaseAlphabet = ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"); 
     String numbers.equals ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"); 
     String symbols = ("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "+", "\", ", "{", "[", ";", ":", "/", "?", ">", ".", "<", ","); 


     System.out.print("Please enter a password: "); 
     outputString = s.nextLine(); 

     System.out.println("Entered Password:\t" + outputString); 
     if (outputString.length() > 8) 
     if (outputString.contains(upperCaseAlphabet)) 
      if (outputString.contains(lowerCaseAlphabet)) 
       if (outputString.contains(numbers)) 
        if (outputString.contains(symbols)) 
     System.out.println("Verdict:\t\t Valid"); 
     else { 
     System.out.println("Vredict:\t\t Invalid"); 
     } 
    } 
} 
+1

您需要了解** Regex **表達式,其顯着更容易。檢查鏈接,它是一個在線網站創建正則表達式! [RegExr](http://regexr.com) – TimeToCode

+0

您需要了解基本的java語法規則。你發佈的代碼甚至沒有編譯 - 你的String聲明是錯誤的。另外:在你的問題上放置如此多的標籤是沒有意義的! – GhostCat

+1

@Bene只要看看他的代碼。它甚至沒有編譯。這段代碼充滿了問題。 – GhostCat

回答

1

您是顯然初學者,所以建議你使用正則表達式解決方案是不可能的。你需要檢查,如果用戶密碼中包含任何大的字符,所以你需要通過整個密碼進行迭代,並檢查是否有任何的字母是在大字母排列,這樣的事情:

public boolean checkIfPasswordHasBigLetter(String password){ 
    List<Character> list = Arrays.asList(upperCaseAlphabet); 
    for(char character : password.toCharArray()){ 
    if(list.contains(character)) 
     return true; 
    } 
    return false; 
} 

同樣用符號和小寫字母等,提取每個單獨的功能到一個新的方法。然後檢查一切是這樣的:

String password = scanner.nextLine(); 
if(hasBigLetter(password) && hasLowLetter(password) && hasEightLetters(password) && hasSymbol(password)) 
    System.out.println("Password is fine"); 
else 
    System.out.println("Password is invalid"); 

Ofcourse這個代碼是遠不完美,但我不會爲你寫的一切,再加上這個問題可以以更簡單的方式來解決,但就像我說的,你是初學者,所以這樣的解決方案會幫助你更多。您應該瞭解Java語法,並使用Eclipse或其他IDE,它會爲語法問題提供很多幫助。

btw。你的字母應該是字符數組,而不是字符串,因爲字母只有一個字符,而不是字符串。

相關問題