I'm trying to write a code that validates an email as a username and a password that has at least one uppercase, one lowercase, and one special character. When I run the program, I'm getting "Successfully signed up." no matter what I input. I believe my error is in my try/catch block.
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.");
}
}
}
}
Here is my solution to it, the explanation is already embedded:
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.");
}
}
Hope this answer helps and please leave this question as it is as someone might need it, unless there is important changes.