简体   繁体   中英

Why will my continue statement not restart my while loop?

I am trying to complete an authentication program for my final project. I am checking for user authentication, if the user info doesn't match the credentials file, the output tells them this and then prompts for username and password while increment an attemptCounter. The only portion I am experiencing an issue with is when I test and incorrect attempt followed by a correct attempt, the while loop will not restart my authentication procedure, instead it simple says incorrect login again. Why isn't my continue statement restarting my while loop iteration?

I tired looking around for this answer on the forum and food nothing specific to my issue. I tried moving my conditional statement as well to see if it was in the wrong spot for my continue to work but it fixed nothing. Compiler says both of my continue statements are unnecessary.

    //prompting user for username
    System.out.println("Please enter your username (\"L\" to logout \"Q\" to quit): ");
    username = scnr.nextLine();
    //evaluating if user wants to quit immediately 
    if(username.equals("Q")) {
        System.exit(0);
    }
    //prompting user for password and storing to password field
    System.out.println("Please enter your password: ");
    password = scnr.nextLine();
    System.out.print("\n");

    //while loop that contains the authentication and authorization logic 
    while (!username.equals("Q") && attemptCounter < 2){
        //calling of hashInput method of MD5Hash class to return the hashed user password
        userHashedPassword = userHash.hashInput(password);
        //while loop to open the credentials for authentication comparison
        while (cfScnr.hasNextLine()) {
            //assigning the files scanned next line to a field for comparison
            line = cfScnr.nextLine();
            //conditional statement to determine if username and password are contained on the line
            //will break file loop as soon as line contains the user's username and password
            //statement logic used to return the role string and remove extra characters and white space
            if (line.contains(username) && line.contains(userHashedPassword)) {
                dqLocation = line.lastIndexOf('"');
                role = line.substring(dqLocation);
                role = role.replaceAll("\\s+", "");
                role = role.replace("\"", "");
                break;
            }
        }
        //conditional statement used to determine if previous loops condtional statement was meant
        //if it wasn't this condition will inform the user of incorrect username and/or password
        //inform them of attempts remaining and prompt them for a new username and password while
        //tracking the attempts and it they want to quit. If Q isn't entered main while loop will restart authentication
        if (role == null){
            attemptCounter++;
            System.out.println("Username or password incorrect. " + (3 - attemptCounter) + " attempts remaining.");
            System.out.println("Please enter your username (\"L\" to logout \"Q\" to quit): ");
            username = scnr.nextLine();
            if(username.equals("Q")) {
                System.exit(0);
            }
            System.out.println("Please enter your password: ");
            password = scnr.nextLine();
            continue;
            }
        //this conditional statement runs only when the user is authenticated
        else {
            //creating new file object and scanner object to scan the role file
            File rFile = new File("src\\zooauthenticationsystem\\" + role + ".txt");
            Scanner rfScnr = new Scanner(rFile);
            //while loop to parse through the role file and output the lines of the file to the console
            while (rfScnr.hasNextLine()){
                rolePrint = rfScnr.nextLine();
                System.out.println(rolePrint);
            }
            //prompting user if they would like to logout or simply quit the program
            System.out.println("\nPress \"L\" to logout and \"Q\" to quit.");
            userDecision = scnr.nextLine();
            //conditional statement to determine their input, and resetting role to null to reset authentication loop conditional statements, restarts main while loop
            if (userDecision.equals("L")){
                System.out.println("Please enter your username: ");
                username = scnr.nextLine();
                if(username.equals("Q")) {
                    System.exit(0);
                }
                System.out.println("Please enter your password: ");
                password = scnr.nextLine();
                System.out.print("\n");
                role = null;
                continue;
            }

Let's get rid of most of your code, let's format the code better, and let's leave only the control structures to see what the continue statements are doing:

while (!username.equals("Q") && attemptCounter < 2) {
    userHashedPassword = userHash.hashInput(password);
    while (cfScnr.hasNextLine()) {
        line = cfScnr.nextLine();
        if (line.contains(username) && line.contains(userHashedPassword)) {
            // ... do some stuff
            break;
        }
    }
    if (role == null) {
        // ... do some stuff
        continue;  // **** (A) ****
    } else {
        // ... do some stuff
        if (userDecision.equals("L")){
            // ... do some stuff
            continue;  // **** (B) ****
        }
    }
}

If line (A) is reached, you're in the if (roll == null) block, the else will never be entered, and the while loop will repeat regardless of the continue statement making continue unnecessary and distracting.

Likewise if line (B) is reached, you're in the last control block of the while loop, and so the loop will continue regardless of the continue statement making continue unnecessary and distracting.

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