我想弄清楚如何检查 String
以验证它是否至少包含一个字母和一个数字。我会提前说这是作业,我有点困惑。
有一种方法 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()
方法,但我不知道怎么做它。
任何指导将不胜感激。在此先感谢您的帮助。
原文由 cdo 发布,翻译遵循 CC BY-SA 4.0 许可协议
这应该工作。