Ihsan Haikal
Ihsan Haikal

Reputation: 1215

Infinite loop during user's selection with switch case and while loop

I am trying to make a CLI with user input to choose from the options available. I am using a switch and while loop to ensure that it can go back to main menu after choosing an option. The problem is that after a user is successfully logged in, it will go to case 4, but it repeats (infinitely) case 4 and I am not sure why.

Here is the code:

public class MainApplication {

    private static User userLoggedIn;
    private static Device deviceLoggedIn;
    private static PrivateKey privateKey;
    private static AbePrivateKey secretKey;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//      CpabeDAO.em.getTransaction().begin();

        System.out.println("Welcome to CPABE Cloud");

        int test = 0;
        while (true){
            switch(test){
            case 0: //Greeting for the user. User now chooses the option.
                System.out.println("Please choose the option by typing the option number:");
                System.out.println("(1) Login");
                System.out.println("(2) Register User");
                System.out.println("(3) Exit");
                int input = scanner.nextInt();
                if(input < 0 ||  input > 3){
                    System.out.println("Unrecognized input.");
                    test = 0;
                }
                else {
                    test = input;
                }
                break;
            case 1:
                System.out.println("Login");
                List<User> userRetrieve = login();
                //check if it is successfully logged in
                boolean loggedIn = (userRetrieve.size() == 0);
                if(!loggedIn) {
                    for(User user : userRetrieve) {
                        userLoggedIn = user;
                    }
                    //Retrieve private key & secret key from cpabe dir, if it is not stored then register new device. After that go to case 4, which is the main menu;
                    test = 4;
                }
                else{ 
                    System.out.println("Your username or password is incorrect. Please choose what to do next:");
                    System.out.println("(1) Login");
                    System.out.println("(2) Back to main menu");
                    int input2 = scanner.nextInt();
                    if(input2 == 1){
                        test = 1;
                    }
                    else if(input2 == 2){
                        test = 0;
                    }
                }
                break;
            case 2:
                System.out.println("Register User");                        

                userLoggedIn = UserRegistration.registerUser();
                 DeviceRegistration.registerNewDeviceforNewUser(userLoggedIn);

                test = scanner.nextInt();   
                break;
            case 3:
                System.out.println("Exit");
                System.out.println("Press 0 to go back to menu");
                test = scanner.nextInt();           
                break;
            case 4: //the main menu. user can have full functionalities after logging in or registering.
                System.out.println("Welcome " + userLoggedIn.getFirstname());
                System.out.println("Please select the option by typing the option number:");
                break;
             default :
                System.out.println("Invalid input. Please insert the correct input.");
                test = 0;
            }
        }
    }

    private static List<User> login(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter your username:");
        String username = scanner.nextLine();
        System.out.println("Please enter your password:");
        String password = scanner.nextLine();       
        String hashPassword = Encryption.generateHash(password.getBytes());
        Query q = CpabeDAO.em.createQuery("SELECT u FROM User u WHERE u.username = '" + username + "' and u.hashPassword = '" + hashPassword + "'");
        List<User> userRetrieve = q.getResultList();
        scanner.close();
        return userRetrieve;
    }

What I want to achieve is that after user is successfully logged in then the system will go to case 4, which is the main menu, and wait for user's input but instead it keeps repeating the case 4.

Upvotes: 1

Views: 2658

Answers (1)

Laurent-P
Laurent-P

Reputation: 145

As @Abubakkar said, you can't break your loop from a switch. Also you stay in the fourth case because you never change the test value, you never ask for another value from the scanner, so it stay at 4. You should put it back to 0 directly from the switch if you want to go back in your menu. You could do that:

    boolean firstMenu = true;
    boolean secondmenu = false;
    while(firstMenu){
        [...]
        case 4: //the main menu. user can have full functionalities after logging in or registering.
            System.out.println("Welcome " + userLoggedIn.getFirstname());
            System.out.println("Please select the option by typing the option number:");
            firstMenu = false;
            secondMenu = true;
            break;
        }
        [...]
    }
    while(secondMenu){
        [Your second menu]
    }

See This, it could help you to go out from the loop, the first and the second answer are great options to fix your problem :)

Upvotes: 1

Related Questions