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.