문자열을 사용자로부터 입력받는 루프가 있다고 가정 해 봅니다. 이 입력을 통해 우리는 특정 기준이 충족되는지 확인하는 일련의 유효성 검증을 설정합니다. 이 모든 조건이 충족되면 문제의 조치가 완료됩니다. 하나; 그렇지 않은 경우 오류를 알리고 프로세스를 다시 시작합니다.루프에서 문자가 아닌 문자에 대한 문자열 검색
제 질문은 문자열에있는 문자의 존재 유무 (또는 존재하지 않는지)를 확인하는 것입니다. 나는이 프로그램을 가지고 있으며이 유효성 검사 중 하나에 대해서는 전체 문자열을 검사해야한다. 문자열에 문자가 아닌 하나 이상의 문자가 없으면 작업을 중단하고 문자가 아닌 문자가 필요하다는 것을 설명합니다.
문제는 if 루프의 표현식에서 어떻게 복제 할 수 있는지 모르겠습니다. 여기 내가 지금까지 가지고있는 것이있다. "문자"로 가정
public static changePassword() // Method that runs through the process of changing the password.
{
// Retrieving the current and new password from the user input.
System.out.println("Welcome to the change password screen.");
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your current password: ");
String currentPassword = keyboard.nextLine();
System.out.print("Please enter the new password: ");
String newPassword1 = keyboard.nextLine();
System.out.print("Please enter the new password again: ");
String newPassword2 = keyboard.nextLine();
// Validating the new password entry.
if (newPassword1.equals(newPassword2)) // Checking to see if the new password was entered exactly the same twice.
{
if (newPassword1.length() >= 6) // Checking to see if the new password has 6 or more characters.
{
if (**some expression**) // Checking to see if the password has at least one non-letter character.
{
currentPassword = newPassword1 // If all conditions are met, it sets the current password to the password entered by the user.
}
else // If there isn't a non-letter character, it informs the user and restarts the process.
{
System.out.println("The new password must have a non-letter character.");
changePassword();
}
}
else // If there is less than 6 characters, it informs the user and restarts the process.
{
System.out.println("The new password can not be less than 6 characters.");
changePassword();
}
}
else // If the new passwords don't match, it informs the user and restarts the process.
{
System.outprintln("The passwords must match.");
changePassword();
}
}
(http://stackoverflow.com/questions/9587907/how-to-check-if-string-has-at-least-one-letter -number-and-special-character-in) –