2011-12-03 8 views
6

Próbuję dowiedzieć się, jak sprawdzić String, aby sprawdzić, czy zawiera co najmniej jedną literę i jedną cyfrę. Będę z góry przekonany, że to praca domowa i jestem trochę zdezorientowany.Metoda Java isLetterOrDigit(), isDigit(), isLetter()

Istnieje metoda metody isLetterOrDigit(), która wydaje się, że jest to właściwe podejście, ale nie jestem pewien, w jaki sposób wprowadziłbym to w moim kodzie. Oto kod używam poniżej:

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."); 

    } 
} 

Chcę zaimplementować metodę gdzie sekcja komentarz jest przy użyciu albo isDigit(), isLetter() lub isLetterOrDigit() metody, ale ja po prostu nie wiem jak to zrobić.

Wszelkie wskazówki będą mile widziane. Z góry dziękuję za pomoc.

Odpowiedz

5

To powinno zadziałać.

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

Ciężko Ci w tym pomóc bez podania całego kodu, ponieważ jest tak krótka.

każdym razie na początek, ponieważ trzeba przynajmniej jedna litera i przynajmniej jeden cyfrowy, będziesz potrzebować dwie flagi, dwie booleans, które będą początkowo false. Można iterację każdego char w ininitialPassword stosując foreach pętlę:

for (char c : initialPassword.toCharArray()) 

I to wszystko co musisz zrobić, to sprawdzić w każdej iteracji jeśli c jest ewentualnie literę lub cyfrę, a jeśli tak ustawić odpowiednią flagę . Po zakończeniu pętli, jeśli obie flagi są ustawione, twoje hasło jest prawidłowe. To jest to, co Twój kod mógłby wyglądać następująco:

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

Poniższy kod jest ostateczny kod, który wymyśliłem Dzięki Państwa sugestie:

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

To wydaje się być stary pytanie i odpowiedział wcześniej, ale dodaję swój kod, ponieważ napotkałem problem z tajskimi znakami akcentowanymi. Więc pracował, aby naprawić ten problem i znalazłem powyższy roztwór, który był niekompletny, jeśli masz do czynienia z takimi postaciami - ก่อน ที่สุด ท้าย o

W celu identyfikacji tych znaków prawidłowo oto kod:

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"); 
    } 

Nie jestem pewien, czy ktoś też musiał się z tym zmierzyć. Mam nadzieję, że to może pomóc.