Próbuję dowiedzieć się, jak sprawdzić String
, aby sprawdzić, czy zawiera co najmniej jedną literę i jedną cyfrę. Będę z góry przekonany, że to praca domowa i jestem trochę zdezorientowany.Metoda Java isLetterOrDigit(), isDigit(), isLetter()
Istnieje metoda metody isLetterOrDigit()
, która wydaje się, że jest to właściwe podejście, ale nie jestem pewien, w jaki sposób wprowadziłbym to w moim kodzie. Oto kod używam poniżej:
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.");
}
}
Chcę zaimplementować metodę gdzie sekcja komentarz jest przy użyciu albo isDigit()
, isLetter()
lub isLetterOrDigit()
metody, ale ja po prostu nie wiem jak to zrobić.
Wszelkie wskazówki będą mile widziane. Z góry dziękuję za pomoc.