我正在尝试编写一个代码来验证电子邮件的用户名和密码,该密码至少有一个大写字母、一个小写字母和一个特殊字符。当我运行这个项目时,我会得到“成功的注册”。不管我输入了什么。我相信我的错误在我的尝试/捕捉块中。
import java.util.Scanner;
public class UserSignUp {
public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
String username;
String password;
try {
System.out.print("Enter your username: ");
username = input1.nextLine();
System.out.print("Enter your password: ");
password = input2.nextLine();
}
catch (IllegalArgumentException e) {
System.out.println(e);
}
System.out.println("Successfully signed up.");
}
public static void validateUsername(String username) throws IllegalArgumentException {
String pattern = ".+@.+\\.com";
if (username.matches(pattern)) {
throw new IllegalArgumentException("Invalid username.");
}
}
public static void validatePassword(String password) throws IllegalArgumentException {
boolean isUppercase = false;
boolean isLowercase = false;
boolean isDigit = false;
boolean hasSpecialCharacter = false;
char ch = 0;
for (int i = 0; i < password.length(); i++) {
ch = password.charAt(i);
if (Character.isUpperCase(ch)) {
isUppercase = true;
} else {
throw new IllegalArgumentException("Password must contain an uppercase letter.");
}
if (Character.isLowerCase(ch)) {
isLowercase = true;
} else {
throw new IllegalArgumentException("Password must contain a lowercase letter.");
}
if (Character.isDigit(ch)) {
isDigit = true;
} else {
throw new IllegalArgumentException("Password must contain a digit.");
}
if (!Character.isDigit(password.charAt(ch)) && !Character.isLetter(password.charAt(ch))) {
hasSpecialCharacter = true;
} else {
throw new IllegalArgumentException("Password must contain a special character.");
}
}
}
}发布于 2022-10-24 06:02:05
以下是我的解决方案,解释已经嵌入:
import java.util.Scanner;
public class UserSignUp {
public static void main(String[] args) {
// No need for two scanners, one scanner can accept multiple (also
// multiple-lined) inputs.
Scanner in = new Scanner(System.in);
try {
// Put validation inside the try block.
// Regarding there are no other use of the variables, it is unnecessary to give
// the two variables "Method Level Scope" instead of "Block Scope".
System.out.print("Enter your username: ");
String username = in.nextLine();
validateUsername(username);
System.out.print("Enter your password: ");
String password = in.nextLine();
validatePassword(password);
// Put the println() inside the try block.
System.out.println("Successfully signed up.");
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
public static void validateUsername(String username) throws IllegalArgumentException {
String pattern = ".+@.+\\.com";
// If not matches the pattern,
if (!username.matches(pattern)) {
throw new IllegalArgumentException("Invalid username.");
}
}
public static void validatePassword(String password) throws IllegalArgumentException {
boolean isUppercase = false;
boolean isLowercase = false;
boolean isDigit = false;
boolean hasSpecialCharacter = false;
char ch = '\0';
for (int i = 0; i < password.length(); i++) {
ch = password.charAt(i);
if (Character.isUpperCase(ch))
isUppercase = true;
if (Character.isLowerCase(ch))
isLowercase = true;
if (Character.isDigit(ch))
isDigit = true;
// ch is already defined as a character.
if (!Character.isDigit(ch) && !Character.isLetter(ch))
hasSpecialCharacter = true;
}
// Ask for the constraints after read through all characters.
if (isUppercase != true)
throw new IllegalArgumentException("Password must contain an uppercase letter.");
else if (isLowercase != true)
// Fix the error message of lower-case letters.
throw new IllegalArgumentException("Password must contain a lowercase letter.");
else if (isDigit != true)
throw new IllegalArgumentException("Password must contain a digit.");
else if (hasSpecialCharacter != true)
throw new IllegalArgumentException("Password must contain a special character.");
}
}希望这个答案对你有帮助,除非有重要的变化,否则请不要回答这个问题,因为有人可能需要它。
https://stackoverflow.com/questions/74176756
复制相似问题