2017-10-04 1 views
0

나는 정규 표현식에를 사용하여 암호를 만들기 위해 노력하고있어 내 코드가 작동하지 않는 이유를 이해하지 않습니다비밀번호

import java.util.Scanner; 

class test { 

public static void main (String[] args) { 

    Scanner scanner = new Scanner(System.in); 

    System.out.println("Enter a password please."); 
     String password = scanner.nextLine(); 

    int x; 
    String redex = "(^[0-9]+$)"; 
    String redexx = "(^[A-Z]+$)"; 

    boolean hasSpace = false; 
    boolean hasUpperCase = false; 
     boolean hasLetter = false; 
     boolean hasDigit = false; // we set four booleans to be able to bring up individual error messages. 

    if (password.length() > 8){ 
     hasLetter = true; 
    } else { 
     System.out.println("Your password needs to be at least 8 characters long.");       

     if (password.matches(redexx)) { // we check to see if the password has at least an upper case letter, one or more digits and a space using regular expressions 
      hasUpperCase = true; 

     } if (password.matches(redexx)) { 
      hasDigit = true; 

     } if (password.indexOf(' ') == 1) { 
      hasSpace = true; } 

    } if (hasLetter && hasDigit && hasUpperCase && !hasSpace) { 
     System.out.println("Your password is strong."); 
    } if (!hasDigit) {  
       System.out.println("You need to put numbers in your password."); 
    } if (!hasUpperCase) { 
     System.out.println("You need to use an upper case letter in your password."); 
    } if (hasSpace) { 
        System.out.println("You need to delete any spaces in your password."); 
     } // if we use if statements (and not any "else" or "else if", we get to show all the possible error messages. 
    } 
} 

를 "정규식"을 수정 한 후와 "regexx"를 컴파일 한 후 완벽하게 적용 할 수있는 암호를 입력해도 암호에 대문자가 필요하고 숫자가 필요합니다.

+4

정규식을 의미합니까? – procrastinator

+4

이 줄이 컴파일되지 않는 이유를 이해합니까? 문자열 redex = (^ [0-9] + $); 문자열 redexx = (^ [A-Z] + $); – Stultuske

+2

'password.length()> 8 '은 패스가 적어도 9 문자 여야 만 테스트를 통과 할 수 있다는 것을 의미합니다. 나중에 3 줄을 쓰면 8이 아닙니다. –

답변

1

이 시도 :

이 질문의 의견에 언급 된 오류 이외의
import java.util.Scanner; 

public class Main { 
    public static void main(String[] args) { 
     Scanner scanner = new Scanner(System.in); 
     System.out.println("Enter a password please."); 
     String password = scanner.nextLine(); 

     boolean validLength = password.length() >= 8; 
     boolean hasLetter = password.matches(".*[a-zA-Z].*"); 
     boolean hasDigit = password.matches(".*\\d.*"); 
     boolean hasSpace = password.matches(".*\\s.*"); 

     if (!validLength) 
      System.out.println("The password must be at least 8 characters long."); 
     else if (!hasLetter) 
      System.out.println("The password must contain at least one letter."); 
     else if (!hasDigit) 
      System.out.println("The password must contain at least one digit."); 
     else if (hasSpace) 
      System.out.println("The password must not contain spaces."); 
     else 
      System.out.println("Your password is strong."); 
    } 
} 

, 당신은 또한 잘못된 정규 표현식에 사용합니다. Java에서 regexes를 사용하는 방법에 대한 정보는 Java regular expression constructs을 읽으십시오.

또한 공백 문자는 실제로 암호를 강하게 만들기 때문에 사용자가 공백 문자를 입력 할 수 있도록하는 것이 좋습니다. 사용자의 요청이기 때문에 사용자가 공백 문자를 입력 할 수는 없지만 내 대답의 데모 코드에서.

0

"정규식"을 넣지 않아서 문제가 발생한 것 같습니다. 따옴표로 묶는다. 그들은 문자열입니다.

그래서 시도 :

String redex = "(^[0-9]+$)"; 
String redexx = "(^[A-Z]+$)";