2014-09-01 3 views
0

문자열을 사용자로부터 입력받는 루프가 있다고 가정 해 봅니다. 이 입력을 통해 우리는 특정 기준이 충족되는지 확인하는 일련의 유효성 검증을 설정합니다. 이 모든 조건이 충족되면 문제의 조치가 완료됩니다. 하나; 그렇지 않은 경우 오류를 알리고 프로세스를 다시 시작합니다.루프에서 문자가 아닌 문자에 대한 문자열 검색

제 질문은 문자열에있는 문자의 존재 유무 (또는 존재하지 않는지)를 확인하는 것입니다. 나는이 프로그램을 가지고 있으며이 유효성 검사 중 하나에 대해서는 전체 문자열을 검사해야한다. 문자열에 문자가 아닌 하나 이상의 문자가 없으면 작업을 중단하고 문자가 아닌 문자가 필요하다는 것을 설명합니다.

문제는 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(); 
     } 


} 
+0

(http://stackoverflow.com/questions/9587907/how-to-check-if-string-has-at-least-one-letter -number-and-special-character-in) –

답변

0

은 그냥 문자열을 반복하고 그 INT 값 문자 범위를 벗어난 문자가 발생하는 경우 true를 반환, A-Z, A-Z에서 영어 문자를 의미한다.

public static boolean containsNonLetter(String s){ 
    for(int i = 0; i < s.length(); i++){ 
     int ind = (int)s.charAt(i); 
     if(ind < 65 || (ind > 90 && ind < 97) || ind > 122) 
      return true; 
    } 
    return false; 
} 
0

나는 문자로 당신이 알파벳을 의미한다고 가정하고 있습니다. regex 패턴을 사용하면 매우 깨끗한 코드를 가질 수있을뿐만 아니라 필요에 따라 패턴을 업데이트 할 수 있습니다. 자세한 내용은 Java Pattern을 확인하십시오. 여기에 코드가 있습니다.

private static final Pattern APLHA = Pattern.compile("\\p{Alpha}"); 

public static boolean hasLetter(String input) { 

    return APLHA.matcher(input).find(); 
} 
이 [링크]를 시도 regex``로 사용할 수 있습니다
관련 문제