2011-12-03 29 views
6

Tôi đang cố gắng tìm ra cách kiểm tra String để xác thực xem nó có ít nhất một chữ cái và một số trong đó hay không. Tôi sẽ trả trước rằng đây là bài tập về nhà và tôi hơi bối rối.Phương thức Java isLetterOrDigit(), isDigit(), isLetter()

Có phương pháp isLetterOrDigit() có vẻ như đó là phương pháp phù hợp, nhưng tôi không chắc chắn như thế nào tôi sẽ thực hiện điều này trong mã của mình. Đây là mã tôi đang sử dụng dưới đây:

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

    } 
} 

Tôi muốn thực hiện một phương pháp mà phần bình luận là sử dụng một trong hai isDigit(), isLetter(), hoặc isLetterOrDigit() phương pháp, nhưng tôi chỉ không biết làm thế nào để làm điều đó.

Mọi hướng dẫn sẽ được đánh giá cao. Cảm ơn trước vì sự giúp đỡ của bạn.

Trả lời

5

Điều này sẽ hiệu quả.

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

Thật khó để giúp bạn làm điều đó mà không cung cấp cho bạn tất cả mã để làm điều đó, vì nó quá ngắn.

Dù sao để bắt đầu, vì bạn cần ít nhất một chữ cái ít nhất một chữ số, bạn sẽ cần hai cờ, hai booleans, ban đầu sẽ là false. Bạn có thể lặp qua từng char trong ininitialPassword bằng cách sử dụng một vòng lặp foreach:

for (char c : initialPassword.toCharArray()) 

Và sau đó tất cả các bạn phải làm là kiểm tra tại mỗi lần lặp nếu c có thể là một lá thư hoặc một chữ số, và đặt cờ tương ứng nếu có . Khi vòng lặp kết thúc, nếu cả hai cờ được đặt, thì mật khẩu của bạn là hợp lệ. Đây là những gì mã của bạn có thể trông giống như:

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

Đoạn code dưới đây là mã cuối cùng mà tôi đã đưa ra nhờ đề xuất của bạn:

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

này có vẻ là cũ câu hỏi và đã được trả lời trước đó, nhưng tôi thêm mã của tôi khi tôi đối mặt với một vấn đề với các ký tự có dấu Thái Lan. Vì vậy, tôi làm việc trên để khắc phục vấn đề đó và tôi tìm thấy giải pháp trên, đó là không đầy đủ nếu bạn đang đối phó với các nhân vật như vậy - ก่อน ที่สุด ท้าย o

Để xác định những nhân vật này một cách chính xác ở đây là mã:

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

Không chắc chắn liệu có ai phải đối mặt với điều này không. Hy vọng điều này có thể giúp đỡ.

Các vấn đề liên quan