I'm trying to create a password verification routine, my problem is when a input is entered it will go through each if statement individually. I'm trying to restart validation when a condition is not met. At the moment it only jumps to the next if statement when the previous condition is met, even though its a new input. Thanks in advance.
import java.util.Scanner; public class passWordCheck { public static void main(String[] args) { String passWord; // Initialize string for the password. Scanner code = new Scanner(System.in); // Give requirements for a valid password. System.out.println("1. A code must contain no spaces"); System.out.println("2. A code must have between 5 and 10 characters (inclusive)"); System.out.println("3. A code must begin with \\ and must end with \\"); System.out.println("4. A code must contain at least one lowercase letter and at least one uppercase letter"); System.out.print("Please enter a code: "); passWord = code.nextLine(); // Do While loop to check all requirement for a valid password are met. do { if (!firstCharacter(passWord)) {// Check First Character System.out.println("Code must start with \\."); System.out.println("Code is invalid, Please try again"); passWord = code.nextLine(); } if (!lastCharacter(passWord)) {// Check last Character System.out.println("Code must end with \\."); System.out.println("Code is invalid, Please try again"); passWord = code.nextLine(); } if (!hasCorrectLength(passWord)) {// Check password length System.out.println("Code must be between 5 and 15 characters in lenth."); System.out.println("Code is invalid, Please try again"); passWord = code.nextLine(); } if (!hasUpperCharacter(passWord)) {// Check for uppercase Character System.out.println("Code must contain at least one uppercase letter."); System.out.println("Code is invalid, Please try again"); passWord = code.nextLine(); } if (!hasLowerCharacter(passWord)) {// Check for lowercase character System.out.println("Code must contain at least one lowercase letter."); System.out.println("Code is invalid, Please try again"); passWord = code.nextLine(); } } while (false); System.out.println("code is valid."); code.close(); } // Methods for checking password requirements public static boolean firstCharacter(String str) { char first = str.charAt(0); // First character of a string if (first == ('\\')) // This verifies there is a \ at the start. { return true; } return false; } public static boolean lastCharacter(String str) { int n = str.length(); // Finding string length char last = str.charAt(n - 1); // Last character of a string if (last == ('\\')) // This verifies there is a \ at the end. { return true; } return false; } public static boolean hasCorrectLength(String str) { int n = str.length(); // Finding string length if (n >= 5 || n <= 15) // This verifies the correct length. { return true; } return false; } public static boolean hasLowerCharacter(String str) { for (int i = 0; i < str.length(); i++) { // For loop to check password for lowercase char currentCharacter = str.charAt(i); if (Character.isLowerCase(currentCharacter)) { return true; } } return false; } public static boolean hasUpperCharacter(String str) { for (int i = 0; i < str.length(); i++) { // For loop to check password for uppercase char currentCharacter = str.charAt(i); if (Character.isUpperCase(currentCharacter)) { return true; } } return false; } }