比较登录表单的两个数组

时间:2022-07-23 12:14:39

I currently have two arrays containing usernames and passwords.

我目前有两个包含用户名和密码的数组。

   String[] UsernameArray = {"John","Per","Daniel","Jonathan"};
   String[] PasswordArray = {"Davis","Harring","Smith","West"};'

Now i have an if statement looking like this:

现在我的if语句看起来像这样:

if(Arrays.asList(UsernameArray).contains(LoginPanel.Username)&& Arrays.asList(PasswordArray).contains(LoginPanel.Password)) {
                   //Do something
    }

Everything is working fine, except for the fact i only want respective people to use their respective last names as passwords. I want for example the username to be "John" and the password to be "Davis" ONLY.

一切都运行正常,除了我只希望各自的人使用他们各自的姓氏作为密码。我希望例如用户名为“John”,密码为“Davis”。

As of now if i enter John and then enter the second persons last name "Harring" i get "Success!" and is logged on. How can i change this so the persons passwords can only be their own lastname and not anyone else's?

截至目前,如果我进入约翰,然后输入第二个人的姓氏“哈林”,我得到“成功!”并登录。如何改变这一点,以便人员密码只能是他们自己的姓氏,而不是其他人的姓名?

6 个解决方案

#1


1  

You need to make sure that the strings are at the same index in their respective arrays. Use the indexOf method:

您需要确保字符串在各自的数组中处于相同的索引处。使用indexOf方法:

if (Arrays.asList(UsernameArray).contains(LoginPanel.Username) &&    
    Arrays.asList(PasswordArray).contains(LoginPanel.Password) && 
    //this way me make sure the username and password are at the same position. 
    Arrays.asList(UsernameArray).indexOf(LoginPanel.Username) == Arrays.asList(PasswordArray).indexOf(LoginPanel.Password) 
)
{
                   //Do something
}

You may want to use a few local variables to avoid so many calls to Arrays.asList, which will create a new list every time you call it.

您可能希望使用一些局部变量来避免对Arrays.asList进行如此多的调用,每次调用它时都会创建一个新列表。

#2


1  

You don't need ArrayLists for that purpose, it's a waste of memory, you can use just arrays.

您不需要ArrayLists用于此目的,这是浪费内存,您可以只使用数组。

Loop over the array and check if the username given is right, save the index of that username and check if the password at that index is the same entered by the user. Something like this:

循环遍历数组并检查给定的用户名是否正确,保存该用户名的索引并检查该索引处的密码是否与用户输入的密码相同。像这样的东西:

for (int i=0; i<usernameArray.length; i++) {
    if (LoginPanel.username.equals(usernameArray[i]) && LoginPanel.password.equals(passwordArray[i])) {
        //right credentials
    }
}

To be clear: inputUser and inputPassword are username and password entered by the user

需要说明的是:inputUser和inputPassword是用户输入的用户名和密码

Note: As you can see, i used usernameArray, passwordArray, LoginPanel.username and LoginPanel.password with lowercase u and p: this is because of java naming conventions, you should start variable names with a lowercase letter.

注意:正如您所看到的,我使用了usernameArray,passwordArray,LoginPanel.username和带有小写u和p的LoginPanel.password:这是因为java命名约定,您应该使用小写字母启动变量名。

#3


0  

Use indexOf:

List<String> usernames = Arrays.asList(UsernameArray);
if (usernames.contains(LoginPanel.Username) &&
    PasswordArray[usernames.indexOf(LoginPanel.Username)].equals(LoginPanel.Password)) {
        // Do something
}

#4


0  

From the API documentation

来自API文档

boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

如果此列表包含指定的元素,则返回true。更正式地,当且仅当此列表包含至少一个元素e时才返回true(o == null?e == null:o.equals(e))。

In your case, UsernameArray contains John and PasswordArray contains Harring and hence Arrays.asList(UsernameArray).contains(LoginPanel.Username)&& Arrays.asList(PasswordArray).contains(LoginPanel.Password) returns true.

在您的情况下,UsernameArray包含John和PasswordArray包含Harring,因此Arrays.asList(UsernameArray).contains(LoginPanel.Username)&& Arrays.asList(PasswordArray).contains(LoginPanel.Password)返回true。

You can use a Map of user name password like

您可以使用用户名密码映射

Map<String,String> map = new HashMap<String,String>();
map.put("John","Davis");

and then this can be validated

然后这可以验证

String password= map.get(LoginPanel.Username);
if(LoginPanel.Password.equals(password0){
        //Do something
}

Please note that in a real web application, password would be stored encrypted in some data store and not in a program.

请注意,在真实的Web应用程序中,密码将加密存储在某些数据存储中而不是程序中。

#5


0  

you could do:

你可以这样做:

for(int i=0;i< UserNameArray.length; i++){
    if(UserNameArray[i].equals(LoginPanel.Username)&& PasswordArray[i].equals(LoginPanel.Password)){
//do something
}

#6


0  

Easy way is that

简单的方法就是这样

public class LoginTestEasy {

public static void main(String[] args) {
    String[] UsernameArray = {"John","Per","Daniel","Jonathan"};
    String[] PasswordArray = {"Davis","Harring","Smith","West"};

    String loginToCheck = "John";
    String passwordToCheck = "Herring";


    boolean loggedIn = false;

    for(int i = 0; i < UsernameArray.length; i++) {
        String currentLogin = UsernameArray[i];
        String currentPass = PasswordArray[i];

        if(currentLogin.equals(loginToCheck) && passwordToCheck.equals(currentPass)) {
            loggedIn = true;
            break;
        }
    }

    System.out.println("Is logged in: " + loggedIn);
}
}

but the better way is to join login and password pair in single object. smth like

但更好的方法是在单个对象中加入登录名和密码对。喜欢

class LoginData {
    String login;
    String pass;

    LoginData(String login, String pass) {
        this.login = login;
        this.pass = pass;
    }

    String getLogin() {
        return login;
    }

    String getPass() {
        return pass;
    }
}

and then to loop among them.

然后在它们之间循环。

#1


1  

You need to make sure that the strings are at the same index in their respective arrays. Use the indexOf method:

您需要确保字符串在各自的数组中处于相同的索引处。使用indexOf方法:

if (Arrays.asList(UsernameArray).contains(LoginPanel.Username) &&    
    Arrays.asList(PasswordArray).contains(LoginPanel.Password) && 
    //this way me make sure the username and password are at the same position. 
    Arrays.asList(UsernameArray).indexOf(LoginPanel.Username) == Arrays.asList(PasswordArray).indexOf(LoginPanel.Password) 
)
{
                   //Do something
}

You may want to use a few local variables to avoid so many calls to Arrays.asList, which will create a new list every time you call it.

您可能希望使用一些局部变量来避免对Arrays.asList进行如此多的调用,每次调用它时都会创建一个新列表。

#2


1  

You don't need ArrayLists for that purpose, it's a waste of memory, you can use just arrays.

您不需要ArrayLists用于此目的,这是浪费内存,您可以只使用数组。

Loop over the array and check if the username given is right, save the index of that username and check if the password at that index is the same entered by the user. Something like this:

循环遍历数组并检查给定的用户名是否正确,保存该用户名的索引并检查该索引处的密码是否与用户输入的密码相同。像这样的东西:

for (int i=0; i<usernameArray.length; i++) {
    if (LoginPanel.username.equals(usernameArray[i]) && LoginPanel.password.equals(passwordArray[i])) {
        //right credentials
    }
}

To be clear: inputUser and inputPassword are username and password entered by the user

需要说明的是:inputUser和inputPassword是用户输入的用户名和密码

Note: As you can see, i used usernameArray, passwordArray, LoginPanel.username and LoginPanel.password with lowercase u and p: this is because of java naming conventions, you should start variable names with a lowercase letter.

注意:正如您所看到的,我使用了usernameArray,passwordArray,LoginPanel.username和带有小写u和p的LoginPanel.password:这是因为java命名约定,您应该使用小写字母启动变量名。

#3


0  

Use indexOf:

List<String> usernames = Arrays.asList(UsernameArray);
if (usernames.contains(LoginPanel.Username) &&
    PasswordArray[usernames.indexOf(LoginPanel.Username)].equals(LoginPanel.Password)) {
        // Do something
}

#4


0  

From the API documentation

来自API文档

boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

如果此列表包含指定的元素,则返回true。更正式地,当且仅当此列表包含至少一个元素e时才返回true(o == null?e == null:o.equals(e))。

In your case, UsernameArray contains John and PasswordArray contains Harring and hence Arrays.asList(UsernameArray).contains(LoginPanel.Username)&& Arrays.asList(PasswordArray).contains(LoginPanel.Password) returns true.

在您的情况下,UsernameArray包含John和PasswordArray包含Harring,因此Arrays.asList(UsernameArray).contains(LoginPanel.Username)&& Arrays.asList(PasswordArray).contains(LoginPanel.Password)返回true。

You can use a Map of user name password like

您可以使用用户名密码映射

Map<String,String> map = new HashMap<String,String>();
map.put("John","Davis");

and then this can be validated

然后这可以验证

String password= map.get(LoginPanel.Username);
if(LoginPanel.Password.equals(password0){
        //Do something
}

Please note that in a real web application, password would be stored encrypted in some data store and not in a program.

请注意,在真实的Web应用程序中,密码将加密存储在某些数据存储中而不是程序中。

#5


0  

you could do:

你可以这样做:

for(int i=0;i< UserNameArray.length; i++){
    if(UserNameArray[i].equals(LoginPanel.Username)&& PasswordArray[i].equals(LoginPanel.Password)){
//do something
}

#6


0  

Easy way is that

简单的方法就是这样

public class LoginTestEasy {

public static void main(String[] args) {
    String[] UsernameArray = {"John","Per","Daniel","Jonathan"};
    String[] PasswordArray = {"Davis","Harring","Smith","West"};

    String loginToCheck = "John";
    String passwordToCheck = "Herring";


    boolean loggedIn = false;

    for(int i = 0; i < UsernameArray.length; i++) {
        String currentLogin = UsernameArray[i];
        String currentPass = PasswordArray[i];

        if(currentLogin.equals(loginToCheck) && passwordToCheck.equals(currentPass)) {
            loggedIn = true;
            break;
        }
    }

    System.out.println("Is logged in: " + loggedIn);
}
}

but the better way is to join login and password pair in single object. smth like

但更好的方法是在单个对象中加入登录名和密码对。喜欢

class LoginData {
    String login;
    String pass;

    LoginData(String login, String pass) {
        this.login = login;
        this.pass = pass;
    }

    String getLogin() {
        return login;
    }

    String getPass() {
        return pass;
    }
}

and then to loop among them.

然后在它们之间循环。