2011-12-03 62 views
6

我想弄清楚如何檢查String來驗證它是否至少有一個字母和一個數字。我會提前說這是作業,我有點困惑。Java isLetterOrDigit()方法,isDigit(),isLetter()

有一種方法isLetterOrDigit()似乎是正確的方法,但我很不自在,因爲我將如何在我的代碼中實現這一點。這裏是我用下面的代碼:

import javax.swing.JOptionPane; 

public class Password 
{ 
    public static void main(String[] args) 
    { 

    String initialPassword; 
    String secondaryPassword; 
    int initialLength; 

    initialPassword = JOptionPane.showInputDialog(null, "Enter Your Passowrd."); 

    initialLength = initialPassword.length(); 

    JOptionPane.showMessageDialog(null, "initialLength = " + initialLength); 

    while (initialLength < 6 || initialLength > 10) 
    { 
     initialPassword = JOptionPane.showInputDialog(null, "Your password does not meet the length requirements. It must be at least 6 characters long but no longer than 10."); 
     initialLength = initialPassword.length(); 
    } 

    //Needs to contain at least one letter and one digit 

    secondaryPassword = JOptionPane.showInputDialog(null, "Please enter your password again to verify."); 

    JOptionPane.showMessageDialog(null, "Initial password : " + initialPassword + "\nSecondar Password : " + secondaryPassword); 

    while (!secondaryPassword.equals(initialPassword)) 
    { 
     secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again."); 
    } 

    JOptionPane.showMessageDialog(null, "The program has successfully completed."); 

    } 
} 

我想實現一個方法,其中的註釋部分則會使用isDigit()isLetter(),或isLetterOrDigit()方法,但我只是不知道如何做到這一點。

任何指導,將不勝感激。預先感謝您的幫助。

回答

5

這應該有效。

public boolean containsBothNumbersAndLetters(String password) { 
    boolean digitFound = false; 
    boolean letterFound = false; 
    for (char ch : password.toCharArray()) { 
    if (Character.isDigit(ch)) { 
     digitFound = true; 
    } 
    if (Character.isLetter(ch)) { 
     letterFound = true; 
    } 
    if (digitFound && letterFound) { 
     // as soon as we got both a digit and a letter return true 
     return true; 
    } 
    } 
    // if not true after passing through the entire string, return false 
    return false; 
} 
0

很難在沒有給你所有的代碼的情況下幫助你做到這一點,因爲它太短了。

反正一開始,因爲你需要至少一個字母至少一個數字,你將需要兩個標誌,二booleans,其最初將false。您可以通過每個charininitialPassword使用foreach循環迭代:

for (char c : initialPassword.toCharArray()) 

然後所有你需要做的就是在每次迭代檢查,如果c可能是一個字母或數字,如果是這樣設置相應的標誌。一旦循環終止,如果兩個標誌都被設置,那麼你的密碼是有效的。這是你的代碼可能是什麼樣子:

boolean bHasLetter = false, bHasDigit = false; 
for (char c : initialPassword.toCharArray()) { 
    if (Character.isLetter(c)) 
     bHasLetter = true; 
    else if (Character.isDigit(c)) 
     bHasDigit = true; 

    if (bHasLetter && bHasDigit) break; // no point continuing if both found 
} 

if (bHasLetter && bHasDigit) { /* valid */ } 
0

下面的代碼是我想出了感謝您的建議最終代碼:

import java.util.Scanner; 

public class Password 
{ 
    public static void main(String[] args) 
    { 

    String initialPassword; 
    String secondaryPassword; 
    int numLetterCheck = 0; 
    int initialLength; 
    boolean digitFound = false; boolean letterFound = false; 


    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Enter a new password: "); 
    initialPassword = keyboard.nextLine(); 

    initialLength = initialPassword.length(); 

    System.out.println("Your initial password length is: " + initialLength); 


    while (initialLength < 6 || initialLength > 10) 
    { 
     System.out.println("Your password does not meet the length requirements of >6 and <10. Please enter a new password."); 
     initialPassword = keyboard.nextLine(); 
     initialLength = initialPassword.length(); 
    } 

    for (char ch : initialPassword.toCharArray()) 
    { 
     if (Character.isDigit(ch)) 
     { 
      digitFound = true; 
     } 
     if (Character.isLetter(ch)) 
     { 
      letterFound = true; 
     } 

     if (digitFound && letterFound) 
     { 
      numLetterCheck = 0; 
     } 
     else 
     { 
      numLetterCheck = 1; 
     } 
    } 

    while (numLetterCheck == 1) 
    { 
     System.out.println("Your password must contain at least one number and one number. Please enter a new passord that meets this criteria: "); 
     initialPassword = keyboard.nextLine(); 

     for (char ch : initialPassword.toCharArray()) 
     { 
      if (Character.isDigit(ch)) 
      { 
       digitFound = true; 
      } 
      if (Character.isLetter(ch)) 
      { 
       letterFound = true; 
      } 

      if (digitFound && letterFound) 
      { 
       numLetterCheck = 0; 
      } 
      else 
      { 
       numLetterCheck = 1; 
      } 
     } 
    } 

    System.out.println("Please enter your password again to verify it's accuracy; "); 
    secondaryPassword = keyboard.nextLine(); 

    System.out.println("Initial password : " + initialPassword + "\nSecondar Password : " + secondaryPassword); 

    while (!secondaryPassword.equals(initialPassword)) 
{ 
    System.out.println("Your passwords do not match. Please enter your password again to verify."); 
    secondaryPassword = keyboard.nextLine();  
} 

System.out.println("The program has successfully completed."); 

} 

}

0

這似乎是老問題,並在早些時候得到了答覆,但由於我面對泰語重音字符的問題,因此我添加了我的代碼。所以,我的工作來解決這個問題,我發現上面的解決辦法,如果你正在處理這樣的字符,這是不完整的 - ก่อนที่สุดท้ายØ

爲了正確地在這裏找出這些字符是代碼:

String value = "abc123ก่อนที่สุด ท้ายo"; 
    // Loop through characters in this String. 
    for (int i = 0; i < value.length(); i++) { 
     char c = value.charAt(i); 

     // See if the character is a letter or not. 
     if (Character.isLetter(c)) { 
     System.out.println("This " + c + " = LETTER"); 
     } 
     if (Character.isDigit(c)) { 
     System.out.println("This " + c + " DIGIT"); 
     } 

     if ((""+c).matches("\\p{M}")) 
      System.out.println("This " + c + " = UNICODE LETTER"); 
    } 

不確定是否有人也遇到過這個問題。希望這可以幫助。