나는 정규 표현식에를 사용하여 암호를 만들기 위해 노력하고있어 내 코드가 작동하지 않는 이유를 이해하지 않습니다비밀번호
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"를 컴파일 한 후 완벽하게 적용 할 수있는 암호를 입력해도 암호에 대문자가 필요하고 숫자가 필요합니다.
정규식을 의미합니까? – procrastinator
이 줄이 컴파일되지 않는 이유를 이해합니까? 문자열 redex = (^ [0-9] + $); 문자열 redexx = (^ [A-Z] + $); – Stultuske
'password.length()> 8 '은 패스가 적어도 9 문자 여야 만 테스트를 통과 할 수 있다는 것을 의미합니다. 나중에 3 줄을 쓰면 8이 아닙니다. –