简体   繁体   中英

Continuing specific loop (inner vs outer)?

I am struggling a bit with some loops I put together for an authentication type system. I am trying to get the code to re-prompt the user to quit if they do not enter 'q', it will display the message that they are still logged in but reverts back to the main loop and asks to input the username again. I was playing with continue and break but so far it will not repeat the inner If statement. Any guidance would be much appreciated.

    package zooauthfinal;


import java.util.Scanner;
import java.io.File;
import java.security.MessageDigest;


public class ZooAuthFinal {

    public static void main (String[] args) throws Exception {

        //Creates new scanner for user input
        Scanner scan = new Scanner(System.in);

        //variable to attempts to track login attempts
        int attempts = 0;

        //While loop to start collecting user credentials for login
        while(true) {
            System.out.print("Enter username: ");
            String user = scan.nextLine(); //reads user input and assigns to variable "user"

            System.out.print("Enter password: ");
            String original = scan.nextLine(); //reads user input and assigns to variable "original" for original password

            //MD5 conversion script
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(original.getBytes());

            byte[] digest = md.digest();

            StringBuffer sb = new StringBuffer();

            for (byte b : digest) {

                sb.append(String.format("%02x", b & 0xff));
            }

                //System.out.println("original:" + original); //Used to check and compare password conversion - shows original user input
        //System.out.println("digested:" + sb.toString()); //Used to check and compare password conversion - shows MD5 conversion of original user input

            //assigns boolean value to variable "done"
            boolean done=false;

            //new scanner to read "credentials.txt" file to authenticate user
            Scanner file = new Scanner (new File("credentials.txt"));

            //While loop to split text file into columns
            while(file.hasNextLine ()) {

                String record = file.nextLine();
                String columns[] = record.split("\t");

                //If statement to match value to username
                if(columns[0].trim().equals(user)) {

    if(columns[1].trim().equals(sb.toString())) {

                             done=true;

                             Scanner sc = new Scanner (new File(columns[3].trim() +".txt"));

                             while(sc.hasNextLine()) {

        System.out.println(sc.nextLine());
                          }

                          break;

                    }
                }
            }

            //If statment to start logout procedure, starts when user has been authenticated and user role message has been delivered based on user role
            if(done) {
                //Instructs user to logout with value 'q'

                System.out.println("Type 'q' to logout: ");

                String ch = scan.nextLine();

                if(ch.toLowerCase().charAt(0) != 'q') {

                    System.out.println("You are still logged in as " + user +".");


                }


                //Assigns user input to lowercase value and checks if matches required 'q'
                else {


                    System.out.println("Successfully logged out. Have a nice day!");


                }

            }



            }
            //If/else to track number of login attempts (limit 3) and adds 1 to each unsuccessful attempt until 3 attempts, then displays exit protocol   
            else {

                attempts ++;

                if(attempts==3) {

                    System.out.println("Could not verify credentials. Goodbye.\n");

                    break;

                }
                //displays login error message on each unsucessful attempt until the third unsuccessful attempt    
                else {

                    System.out.println("Invalid username or password, please try again.");
                }


                }
            }
        }

在if(done)块的最后一行添加break语句。

if(!ch.toLowerCase().equals("q"))

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