Can anyone teach me how can when user type -1 exit the program and password have mistake re-entry the password and I have the big problem for this code I dun know why user type error password but java still show Password is valid but when i use else if that will be only display one thing of my rule thanks
package test;
import java.util.Scanner;
class PasswordValidator {
public static void main(String[] args) {
while(true) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter user name : ");
String username = sc.nextLine();
System.out.print("Enter password : ");
String password;
boolean hasLength;
boolean hasUppercase;
boolean hasLowercase;
boolean hasDigit;
boolean hasSpecial;
boolean whitespace;
boolean userpass;
password = sc.nextLine();
hasLength = password.length() > 10;
hasLength = password.length() < 7;
hasUppercase = !password.equals(password.toUpperCase());
hasLowercase = !password.equals(password.toLowerCase());
hasDigit = password.matches(".*[0-9].*");// checks for digits
hasSpecial = !password.matches("[A-Za-z0-9]*"); // for anything not a letter in the ABC's
whitespace = !password.matches(".*\\s.*");
userpass = !password.matches(username);
if (hasLength) {
System.out.println("Password should be within 7 to 10 characters in length");
}
if (!hasUppercase) {
System.out.println("Password should be at least one upper-case alphabet");
}
if (!hasLowercase) {
System.out.println("Password should be at least one lower-case alphabet");
}
if (!hasDigit) {
System.out.println("Password should cantain at least one number");
}
if (!hasSpecial) {
System.out.println("Password should be at least one special character");
}
if (!whitespace) {
System.out.println("Password should not contain whitespace.");
}
if (!userpass) {
System.out.println("Password should not contain or be the same as user name.");
}
else {
System.out.println("Password is valid.");
}
}}}