简体   繁体   English

尝试从ArrayList中获取多个用户名和密码

[英]Trying to grab multiple usernames and passwords from an ArrayList

So I'm having difficulties figuring out why this isn't working but basically what I'm trying to do is grab user information so when they click login they are able to login. 因此,我很难弄清为什么它不起作用,但基本上我想做的就是获取用户信息,以便他们单击登录后就可以登录。

private boolean login() {
    for (User user : users) {
        if (user != null) {
            System.out.println(users.size());
            String tryUser = user.getUsername();
            String tryPass = user.getPassword();
            String passText = new String(
                    LoginPanel.passwordPf.getPassword());

            if (LoginPanel.usernameTf.getText().equalsIgnoreCase(
                    tryUser)
                    && passText.equals(tryPass)) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}

I checked the array and all the users are stored properly but for some reason when I try to get the username and password it grabs it from the first item in the list. 我检查了数组,所有用户均被正确存储,但是由于某种原因,当我尝试获取用户名和密码时,它从列表中的第一项中获取了它。

You are always return ing in the loop, so you will never check any user beyond the first one. 您总是在循环中return ,因此您将永远不会检查第一个用户以外的任何用户。 You should change your check: 您应该更改支票:

if (LoginPanel.usernameTf.getText().equalsIgnoreCase(tryUser) && passText.equals(tryPass)) {
  return true;
}
// Do not return, check next user in list
// else {
//   return false;
// }

Although it may seem intuitive to do the following: 尽管执行以下操作似乎很直观:

} else {
    return false;
}

You should use "continue" to run your for loop over the next element. 您应该使用“ continue”在下一个元素上运行for循环。

} else {
    continue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM