简体   繁体   中英

Issues with proper looping in a do while loop - Java

I seem to be having some issues with my program. I was advised to use a do while loop by my instructor to keep the program running. However, it errors out when I type in an invalid password and restarts. When I input a valid password it only prints "Valid Password" and does not loop. Where have I gone wrong?

import java.util.Scanner;

public class CovenduniteProg5_FIX {

    public static void main(String[] args)      {
        boolean passwordValid = false;

        do  {
            boolean invalidLength = false;
            boolean containsRestrictedWord = false;
            boolean containsLowerCaseLetter = false;
            boolean containsUpperCaseLetter = false;
            boolean containsDigit = false;
            boolean containsSpecialChar = false;

            Scanner stdIn = new Scanner(System.in);

            System.out.println("Password Verifier"); 
            System.out.println("\nEnter a password that meets the following rules:\n" +
                "\tIs at least 8 characters long\n" + 
                "\tContains at least 1 lower letter character\n" +
                "\tContains at least 1 upper letter character\n" +
                "\tContains at least 1 numberic digit\n" +
                "\tContains at least 1 special character from the set: !@#$%^&*\n" +
                "\tDoes not contain the word \"and\" or the word \"the\"\n");
            System.out.print("Enter your password: ");
            String password = stdIn.nextLine();

            for (int i = 0; i < password.length(); i++) {
                char ch = password.charAt(i);

                if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
                    containsUpperCaseLetter = true;
                }

                if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
                    containsLowerCaseLetter = true;
                }

                if (Character.isDigit(ch) && !containsDigit) {
                    containsDigit = true;
                }

                if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&' || ch == '*') && !containsSpecialChar) {
                    containsSpecialChar = true;
                }
            }

            if (password.length() < 8 ) {
                invalidLength = true;
            } 

            if (password.contains("and") || password.contains("the")) {
                containsRestrictedWord = true;
            }

            if (invalidLength) {
                System.out.println("Invalid: Invalid length");
            }

            if (containsRestrictedWord) {
                System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
            }

            if (!containsDigit) {
                System.out.println("Invalid: Does not contain at least one digit.");
            }

            if (!containsLowerCaseLetter) {
                System.out.println("Invalid: Does not contain at least one lowercase letter.");
            }

            if (!containsUpperCaseLetter) {
                System.out.println("Invalid: Does not contain at least one uppercase letter.");
            }

            if(!containsSpecialChar) {
                System.out.println("Invalid: Does not contain at least one special character.");
            }

            if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit && !containsRestrictedWord && !invalidLength)
                passwordValid = true;

            if (passwordValid)
                System.out.print("\nPassword is valid.");

        } while (!passwordValid);

        passwordValid = false;

        while(!passwordValid) {
            if (passwordValid)
                System.out.print("\nPassword is valid.");
        }
    }
}

You put the last passwordValid = false; outside the do-while when you should just have it once before the loop. And you also do not need the second while(!passwordValid) loop:

boolean passwordValid = false; //← only here
do  {
    boolean invalidLength = false;
    boolean containsRestrictedWord = false;
    boolean containsLowerCaseLetter = false;
    boolean containsUpperCaseLetter = false;
    boolean containsDigit = false;
    boolean containsSpecialChar = false;

    Scanner stdIn = new Scanner(System.in);

    /*

    ...

    */

    if (/*All sub-conditions met*/)
        passwordValid = true;

    if (passwordValid)
        System.out.print("\nPassword is valid.");
}
while (!passwordValid);
//passwordValid = false; ← remove this

//no need for while or if statment, because you can only reach this code if passwordValid = true
System.out.print("\nPassword is valid.");

We don't need the second while loop, it's basically an infinite loop . Also, we need to move the "Password Verifier" statements outside the while loop as they needn't be printed multiple times. So, the method will look something like this:

public static void main(String[] args) {

    boolean passwordValid = false;
    System.out.println("Password Verifier");
    System.out.println("\nEnter a password that meets the following rules:\n" + "\tIs at least 8 characters long\n"
            + "\tContains at least 1 lower letter character\n" + "\tContains at least 1 upper letter character\n"
            + "\tContains at least 1 numberic digit\n"
            + "\tContains at least 1 special character from the set: !@#$%^&*\n"
            + "\tDoes not contain the word \"and\" or the word \"the\"\n");
    do {
        boolean invalidLength = false;
        boolean containsRestrictedWord = false;
        boolean containsLowerCaseLetter = false;
        boolean containsUpperCaseLetter = false;
        boolean containsDigit = false;
        boolean containsSpecialChar = false;

        Scanner stdIn = new Scanner(System.in);

        System.out.print("Enter your password: ");
        String password = stdIn.nextLine();

        for (int i = 0; i < password.length(); i++) {
            char ch = password.charAt(i);

            if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
                containsUpperCaseLetter = true;
            }

            if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
                containsLowerCaseLetter = true;
            }

            if (Character.isDigit(ch) && !containsDigit) {
                containsDigit = true;
            }

            if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&'
                    || ch == '*') && !containsSpecialChar) {
                containsSpecialChar = true;
            }
        }

        if (password.length() < 8) {
            invalidLength = true;
        }

        if (password.contains("and") || password.contains("the")) {
            containsRestrictedWord = true;
        }

        if (invalidLength) {
            System.out.println("Invalid: Invalid length");
        }

        if (containsRestrictedWord) {
            System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
        }

        if (!containsDigit) {
            System.out.println("Invalid: Does not contain at least one digit.");
        }

        if (!containsLowerCaseLetter) {
            System.out.println("Invalid: Does not contain at least one lowercase letter.");
        }

        if (!containsUpperCaseLetter) {
            System.out.println("Invalid: Does not contain at least one uppercase letter.");
        }

        if (!containsSpecialChar) {
            System.out.println("Invalid: Does not contain at least one special character.");
        }

        if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit
                && !containsRestrictedWord && !invalidLength)
            passwordValid = true;

        if (passwordValid)
            System.out.print("\nPassword is valid.");

    } while (!passwordValid);
}

The following code will do what you ask. Some things to consider :

  • The second while loop is infinite.
  • Declare the scanner outside the do loops...creating one everytime could cause issues and you only need to declare and create it once.
  • Use two do loops, the outer loop has an infinite condition so it will continue forever...you can re-write to terminate in some fashion.
  • the outer loop displays your menu.
  • the inner loop does all you scanner/input and validation and determination.

Hope this helps

// CovenduniteProg5_FIX
public static void main(String[] args) {
    boolean passwordValid = false;
    Scanner stdIn = new Scanner(System.in);

    do {
        System.out.println("Password Verifier");
        System.out.println("\nEnter a password that meets the following rules:\n"
                + "\tIs at least 8 characters long\n" + "\tContains at least 1 lower letter character\n"
                + "\tContains at least 1 upper letter character\n" + "\tContains at least 1 numberic digit\n"
                + "\tContains at least 1 special character from the set: !@#$%^&*\n"
                + "\tDoes not contain the word \"and\" or the word \"the\"\n");
        System.out.print("Enter your password: ");

        do {
            boolean invalidLength = false;
            boolean containsRestrictedWord = false;
            boolean containsLowerCaseLetter = false;
            boolean containsUpperCaseLetter = false;
            boolean containsDigit = false;
            boolean containsSpecialChar = false;

            try { 
                String password = stdIn.nextLine();

                for (int i = 0; i < password.length(); i++) {
                    char ch = password.charAt(i);

                    if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
                        containsUpperCaseLetter = true;
                    }

                    if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
                        containsLowerCaseLetter = true;
                    }

                    if (Character.isDigit(ch) && !containsDigit) {
                        containsDigit = true;
                    }

                    if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&'
                            || ch == '*') && !containsSpecialChar) {
                        containsSpecialChar = true;
                    }
                }

                if (password.length() < 8) {
                    invalidLength = true;
                }

                if (password.contains("and") || password.contains("the")) {
                    containsRestrictedWord = true;
                }

                if (invalidLength) {
                    System.out.println("Invalid: Invalid length");
                }

                if (containsRestrictedWord) {
                    System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
                }

                if (!containsDigit) {
                    System.out.println("Invalid: Does not contain at least one digit.");
                }

                if (!containsLowerCaseLetter) {
                    System.out.println("Invalid: Does not contain at least one lowercase letter.");
                }

                if (!containsUpperCaseLetter) {
                    System.out.println("Invalid: Does not contain at least one uppercase letter.");
                }

                if (!containsSpecialChar) {
                    System.out.println("Invalid: Does not contain at least one special character.");
                }

                if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit
                        && !containsRestrictedWord && !invalidLength)
                    passwordValid = true;

                if (passwordValid)
                    System.out.print("\nPassword is valid.");

            }
            catch ( Exception ltheXcp ) { 
                ltheXcp.printStackTrace();
            }
        } while (!passwordValid);

        passwordValid = false;

    } while ( 1 != 2 );

//        while (!passwordValid) {
//            if (passwordValid)
//                System.out.print("\nPassword is valid.");
//        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM