我想弄清楚如何檢查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()
方法,但我只是不知道如何做到這一點。
任何指導,將不勝感激。預先感謝您的幫助。