I've been trying to create a password verification using java. The requirements for the password verification should be:
1) The password should be at least six characters long.
2) The password should at least contain one uppercase and lowercase.
3) The password should have at least one digit.
I have done both 1 and 2, but I can't figure out number 3.
I have made a boolean method for checking characters if it has digits. Supposedly, it'll return true if it was able to find a digit, but even if I have entered a password with a digit, it still returns false.
I wonder what's wrong with my code. Hope you guys can help me with this.
package PasswordVerifier;
import java.util.Scanner;
public class PasswordVerifierMain {
public static void main(String[]args){
Scanner hold = new Scanner(System.in);
String pass;
System.out.print("Enter password:");
pass = hold.nextLine();
if(isValid(pass) && pass.length() >= 6){
boolean hasUppercase = pass.equals(pass.toUpperCase());
boolean hasLowercase = pass.equals(pass.toLowerCase());
if(!hasUppercase){
System.out.print("Must have atleast one uppercase letter!");
}else if(!hasLowercase){
System.out.print("Must have atleast one lowercase letter!");
}else{
System.out.print("Password Successful!");
}
}else{
System.out.print("Invalid Password! Minimum six characters!");
}
System.out.println();
System.out.println(isValid(pass));
}
private static boolean isValid(String pass){
boolean status = true;
int i = 0;
while(status && i < pass.length()){
if(!Character.isDigit(pass.charAt(i))){
status = false;
}
i++;
}
return status;
}
}
boolean hasUppercase = pass.equals(pass.toUpperCase());won't work. Assume a password likeaBc123.toUpperCase()would result inABC123which is not equal, but the password still contains an upper case character. The same will be true fortoLowerCase().