2011-12-03 9 views
6

Sto cercando di capire come controllare un String per verificare se ha almeno una lettera e un numero al suo interno. Sarò in anticipo che questo è compito a casa e io sono un po 'confuso.Java isLetterOrDigit() metodo, isDigit(), isLetter()

C'è un metodo isLetterOrDigit() che sembra essere l'approccio giusto, ma non sono sicuro di come implementarlo nel mio codice. Ecco il codice che sto usando qui di seguito:

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

    } 
} 

Voglio realizzare un metodo in cui la sezione di commento è utilizzando i isDigit(), isLetter() o isLetterOrDigit() metodi, ma io proprio non so come farlo.

Qualsiasi consiglio sarebbe apprezzato. Grazie in anticipo per l'assistenza.

risposta

5

Questo dovrebbe funzionare.

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

È difficile aiutarti a farlo senza fornirti tutto il codice per farlo, dato che è molto breve.

In ogni caso per un inizio, in quanto è necessario almeno una lettera e almeno una cifra, si sta andando ad avere bisogno di due bandiere, due booleans, che sarà inizialmente false. È possibile eseguire iterazioni ogni char in ininitialPassword utilizzando un foreach ciclo:

for (char c : initialPassword.toCharArray()) 

E poi tutto quello che dovete fare è controllare ad ogni iterazione se c è forse una lettera o una cifra, e impostare il flag corrispondente in caso affermativo . Una volta che il ciclo termina, se sono impostati entrambi i flag, allora la tua password è valida. Questo è ciò che il vostro codice potrebbe essere simile:

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

Il codice qui sotto è il codice finale che mi è venuta grazie ai vostri suggerimenti:

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

questo sembra essere vecchio domanda ed è stata data una risposta in precedenza, ma sto aggiungendo il mio codice in quanto ho affrontato un problema con i caratteri accentati thailandesi. Così ho lavorato per risolvere questo problema e ho trovato la soluzione di cui sopra, che era incompleto, se avete a che fare con tali personaggi - ก่อน ที่สุด ท้าย o

Al fine di identificare questi caratteri correttamente ecco il codice:

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

Non sono sicuro che qualcuno abbia affrontato anche questo. Spero che questo possa aiutare.