简体   繁体   中英

I'm having trouble with a java login menu app

I have a project in college to create a login menu type application.

I am very much a beginner so please bear with me. Can I ask for someone to point me in the right direction as I've hit a bit of a blank on this one.

The application isn't finished so I understand there will be more that needs adding to it, just know that it is only a very barebones application I am looking to build.

After choosing option 1, the app tells the user to first change password and attempts. After changing the password and the attempts (I'm changing it to 5 when testing) and re-choosing option 1, this error comes up -

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at loginmenu.LoginMenu.loginAttempt(LoginMenu.java:74)
at loginmenu.LoginMenu.showMenu(LoginMenu.java:34)
at loginmenu.LoginMenu.loginAttempt(LoginMenu.java:77)
at loginmenu.LoginMenu.showMenu(LoginMenu.java:34)
at loginmenu.LoginMenu.main(LoginMenu.java:12)

Here is the code -

package loginmenu;

import java.util.Scanner;

public class LoginMenu {

    private static String correctPassword;
    public static String userPassword;
    public static int attemptsCounter;
    public static boolean loggedIn;

    public static void main(String[] args) {
        showMenu();
        loginAttempt();

    }

    public static void showMenu()
    // displays a menu and keeps displaying until user chooses the QUIT option
    {
        int userChoice;
        Scanner myScan = new Scanner(System.in);
        do {
            System.out.println("1.  Login");
            System.out.println("2.  Change Password");
            System.out.println("3.  Change Attempts");
            System.out.println("4.  Quit");
            userChoice = myScan.nextInt();

            switch (userChoice) {
            case 1: {
                System.out.println("You chose to login.");
                loginAttempt();
                break;
            }
            case 2: {
                System.out.println("You chose to change password.");
                Scanner myNewScan = new Scanner(System.in);
                System.out.println("Please enter a password.");
                userPassword = myNewScan.nextLine();

                break;
            }

            case 3: {
                System.out.println("You chose to change attempts.");
                Scanner myNewScan = new Scanner(System.in);
                System.out.println("Please enter amount of attempts.");
                attemptsCounter = myNewScan.nextInt();
                break;
            }
            case 4: {
                System.out.println("You have quit the program.");
                break;
            }
            default: {
                System.out.println("Not a valid choice.");
            }
            }// closes switch
        } while (userChoice != 4);
        myScan.close();
        System.out.println("Goodbye.");
    }// closes showMenu1

    public static void loginAttempt()
    {
        Scanner scan = new Scanner(System.in);

        while (correctPassword != userPassword) && (attemptsCounter>=5);
            {
                System.out.println("Please change password and attempts first."); 
                showMenu();
            }

        if (userPassword == correctPassword) && (attemptsCounter<=5)
            {
                System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
            }
        if (attemptsCounter>=6)
            {
                System.out.println("You have been locked out.");
            }
    }
}

Fix your brackets and ditch the semicolon and don't use == / != for strings.

while (correctPassword != userPassword) && (attemptsCounter>=5);

should be

while (!correctPassword.equals(userPassword) && (attemptsCounter>=5))

same issue with brackets here:

if (userPassword == correctPassword) && (attemptsCounter<=5)

This is my approach of your question.

First, i set the my password at the first place.

private static String correctPassword = "tommybee";

And, i can call the showMenu method only in this case.

public static void main(String[] args) {
    showMenu();
}

You don't have to call the showMenu method in your loginAttempt and the while statement is also removed.

public static void loginAttempt() {
    if (!(userPassword.toLowerCase().equals(correctPassword)) && (attemptsCounter >= 5)) {
        System.out.println("Please change password and attempts first.");
    }

    if ((correctPassword.toLowerCase().equals(userPassword)) && (attemptsCounter <= 5)) {
        System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
    }

    if (attemptsCounter >= 6) {
        System.out.println("You have been locked out.");
    }
}

In the showMenu method, i use only one scanner class with system's input stream. Check the scanner class .

Declare a Scanner class and initialize a userChoice variable to 4.

Scanner myScan = new Scanner(System.in);
int userChoice = 4;

Here is showMenu method.

public static void showMenu()
// displays a menu and keeps displaying until user chooses the QUIT option
{

    Scanner myScan = new Scanner(System.in);
    int userChoice = 4;

    do {
        System.out.println("Choose one of the list below");
        System.out.println("1.  Login");
        System.out.println("2.  Change Password");
        System.out.println("3.  Change Attempts");
        System.out.println("4.  Quit");


        if(myScan.hasNextInt())
        {
            userChoice = myScan.nextInt();

            switch (userChoice) {
                case 1: {
                    System.out.println("You choose to login.");

                    System.out.println("Please enter a password.");

                    userPassword = myScan.next();
                    System.out.println("pass " + userPassword);
                    loginAttempt();

                    break;
                }
                case 2: {
                    System.out.println("You choose to change password.");
                    System.out.println("Please enter a new password.");
                    correctPassword = myScan.next();

                    break;
                }

                case 3: {
                    System.out.println("You choose to change attempts.");
                    System.out.println("Please enter amount of attempts.");
                    attemptsCounter = myScan.nextInt();
                    break;
                }
                case 4: {
                    System.out.println("You have quit the program.");
                    break;
                }
                default: {
                    System.out.println("Not a valid choice.");
                }
            }// closes switch
        }

    } while (myScan.hasNext() && userChoice != 4);

    myScan.close();
    System.out.println("Goodbye.");

}// closes showMenu1

I use a next method instead of a nextInt method. See the next method of the api document.

This method may block while waiting for input to scan,

Here is what i have done.

import java.util.Scanner;

public class LoginMenu {

    private static String correctPassword = "tommybee";
    public static String userPassword;
    public static int attemptsCounter;
    public static boolean loggedIn;

    public static void main(String[] args) {
        showMenu();
        //loginAttempt();

    }

    public static void showMenu()
    // displays a menu and keeps displaying until user chooses the QUIT option
    {

        Scanner myScan = new Scanner(System.in);
        int userChoice = 4;

        do {
            System.out.println("Choose one of the list below");
            System.out.println("1.  Login");
            System.out.println("2.  Change Password");
            System.out.println("3.  Change Attempts");
            System.out.println("4.  Quit");


            if(myScan.hasNextInt())
            {
                userChoice = myScan.nextInt();

                switch (userChoice) {
                    case 1: {
                        System.out.println("You choose to login.");

                        System.out.println("Please enter a password.");

                        //Scanner myNewScan = new Scanner(System.in);
                        userPassword = myScan.next();
                        //myNewScan.close();
                        System.out.println("pass " + userPassword);
                        loginAttempt();

                        break;
                    }
                    case 2: {
                        System.out.println("You choose to change password.");
                        //Scanner myNewScan = new Scanner(System.in);
                        System.out.println("Please enter a new password.");
                        correctPassword = myScan.next();
                        //myNewScan.close();

                        break;
                    }

                    case 3: {
                        System.out.println("You choose to change attempts.");
                        //Scanner myNewScan = new Scanner(System.in);
                        System.out.println("Please enter amount of attempts.");
                        attemptsCounter = myScan.nextInt();
                        //myNewScan.close();
                        break;
                    }
                    case 4: {
                        System.out.println("You have quit the program.");
                        break;
                    }
                    default: {
                        System.out.println("Not a valid choice.");
                    }
                }// closes switch
            }

        } while (myScan.hasNext() && userChoice != 4);

        myScan.close();
        System.out.println("Goodbye.");

    }// closes showMenu1

    public static void loginAttempt() {

        //while ((correctPassword != userPassword) && (attemptsCounter >= 5)) {
        if (!(userPassword.toLowerCase().equals(correctPassword)) && (attemptsCounter >= 5)) {
            System.out.println("Please change password and attempts first.");
            //showMenu();
        }

        if ((correctPassword.toLowerCase().equals(userPassword)) && (attemptsCounter <= 5)) {
            System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
        }

        if (attemptsCounter >= 6) {
            System.out.println("You have been locked out.");
        }
    }
}

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