简体   繁体   中英

Java login system does not work; reads the first user but cannot parse the second or the next ones

I have made a login system using arrays. I have made separate arrays to store users and admins (usernames stored in users and admins and passwords stored in usersPswd and adminsPswd)

static public String users[] = new String[10];
static public String usersPswd[] = new String[10];
static public String admins[] = new String[10];
static public String adminsPswd[] = new String[10];

I have also made two variables that increments up each time a user is created to store the user in the correct index as well as a status string corresponding to the logged in status

static public int userTrackerCount = 0;
static public int adminTrackerCount = 0;
static public String status = "";

What I do to login is set the users at index count to the username and the userspswd at index count to the password every time the register user method is run (assuming username and password have values prior to this code being run from scanner input):

users[uCount] = username;
usersPassword[uCount] = password;

or the register admin:

admins[count] = username;
adminsPassword[count] = password;

This is my login method:

public void login(String username, String password, String hierarchy) {
    if ("admin".equals(status) || "user".equals(status)) {
        System.out.println("You are already logged in");
        return;
    } else {
        while ("".equals(status)) {
            if (hierarchy.equals("admin")) {
                for (int i = 0; i < admins.length; i++) {
                    if (username.equals(admins[i])) {
                        for (int a = 0; a < adminsPassword.length; a++) {
                            if (password.equals(adminsPassword[i])) {
                                System.out.println("You have successfully 
logged in!");
                                status = "admin";
                                currentUserIndex = i;
                                return;
                            } else {
                                System.out.println("Wrong password");
                                return;
                            }
                        }
                    } else {
                        System.out.println("Admin not recognized");
                        return;
                    }
                }
            } else if (hierarchy.equals("user")) {
                for (int i = 0; i < users.length; i++) {
                    if (username.equals(users[i])) {
                        for (int a = 0; a < usersPassword.length; a++) {
                            if (password.equals(usersPassword[i])) {
                                System.out.println("You have successfully 
logged in!");
                                status = "user";
                                currentUserIndex = i;
                                return;
                            } else {
                                System.out.println("Wrong password");
                                return;
                            }
                        }
                    } else {
                        System.out.println("User not recognized");
                        return;
                    }
                }
            }

        }
    }

EDIT: I also have a logout method that sets the status back to "" and I run this method consequently after logging in to each user For example Login Logout Login (again)

When I run the login method after registering two users, the first user is logged in but the second user cannot be parsed (runs the message "User cannot recognized"). This is strange because I checked the users and usersPswd arrays after registering the users and the both the usernames and passwords were there. Please help with this issue. If you would like to see more code I may not be able to provide it (as this is for a final project and there is a plagiarism checker)

You should learn how to use debugger it will help you.

The problem is within this block of code:

 1 for (int i = 0; i < users.length; i++) {
 2   if (username.equals(users[i])) {
 3     for (int a = 0; a < usersPassword.length; a++) {
 4       if (password.equals(usersPassword[i])) {
 5         System.out.println("You have successfully logged in!");
 6         status = "user";
 7         currentUserIndex = i;
 8         return;
 9       } else {
10         System.out.println("Wrong password");
11         return;
12       }
13     }
14   } else {
15     System.out.println("User not recognized");
16     return;
17   }
18 }

When second user call login() and it is first loop condition at line 2 will return false (second user has different name than first one). As a result else from line 14 will be called.

To correct the solution you could remove else at line 14 and add if block after for loop:

//no changes before
} else if (hierarchy.equals("user")) {
    for (int i = 0; i < users.length; i++) {
        if (username.equals(users[i])) {
            for (int a = 0; a < usersPassword.length; a++) {
                if (password.equals(usersPassword[i])) {
                    System.out.println("You have successfully logged in!");
                    status = "user";
                    currentUserIndex = i;
                    return;
                } else {
                    System.out.println("Wrong password");
                    return;
                }
            }
        }
    }
    if (!status.equals("user")) {
        System.out.println("User not recognized");
        return;
    }
}
//no changes after

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