将字符串与字符串数组进行比较

发布于 2024-12-14 15:32:48 字数 553 浏览 1 评论 0原文

基本上我想做的是一个非常简单的登录屏幕来了解 swing。

我的问题是我目前有一个文件 pass.txt ,其格式如下:

Username = bob,tony,mike
Password = pass,pass2,pass3

在我的 Java 文件中,我通过使用以下方式获取字符串:

String[] user = prop.getProperty("Username").split(",");

现在,我将其与来自 JTextField 的文本输入进行比较 但是它总是失败我所拥有的是:

if (input2.equals(pass) && userin.getText().equals(user))

现在我猜我的问题是我有一个字符串数组,它正在将它与单个字符串进行比较,现在我想做的是遍历该数组,如果有的话他们匹配我希望它接受该匹配并在以下情况下使用它这是有道理的,有什么办法可以解决这个问题吗?

Right basically what I'm trying to do is a very simple login screen to get to know swing.

My issue is I currently have a file pass.txt which is formatted like so:

Username = bob,tony,mike
Password = pass,pass2,pass3

in my Java file I get the strings by using:

String[] user = prop.getProperty("Username").split(",");

Now I then compare this with my text input from a JTextField however it always fails what I have is:

if (input2.equals(pass) && userin.getText().equals(user))

Now I'm guessing my issue is I have an array of strings and it's comparing it to a single string now what I want to do is go through the array and if any of them match I want it to take that match and use it if that makes sense, is there any way to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-12-21 15:32:48

我认为这可能对您有帮助,因为我认为您必须在这个数组中检查每个用户的密码:

for(int i=0;i < user.size();i++){
  if(input2.equals(pass[i]) && userin.getText().equals(user[i])){
   //your code
  }
}

i think this may help you,because i think you have to check each user with his password int this array:

for(int i=0;i < user.size();i++){
  if(input2.equals(pass[i]) && userin.getText().equals(user[i])){
   //your code
  }
}
只有影子陪我不离不弃 2024-12-21 15:32:48

假设您有一个 pass 数组来匹配您的 user 数组,并且 user 中的每个条目都保证在 pass 中具有相应的条目,那么以下解决方案应该有效:

int index = Arrays.asList(user).indexOf(userin.getText());
String password = pass[index];

if (password.equals(input2)) {
    // Successful authentication
} else {
    // Authentication failed
}
  • Arrays.asList(user).indexOf(userin.getText()) 将获取以下索引
    列表中的用户(在您的示例中,“bob”=> 0;“tony”=> 1;“mike”=> 2)。
  • password 是同一索引处的密码字符串(在您的示例中,
    “通过”=> 0; “pass2”=>1; “pass3” => 2)。
  • 然后 if 比较与用户关联的密码
    (password) 与在对话框中输入的密码
    输入2)。

Assuming you have a pass array to match your user array, and every entry in user is guaranteed to have a corresponding entry in pass, then the following solution should work:

int index = Arrays.asList(user).indexOf(userin.getText());
String password = pass[index];

if (password.equals(input2)) {
    // Successful authentication
} else {
    // Authentication failed
}
  • Arrays.asList(user).indexOf(userin.getText()) will get the index of
    the user in the list (in your example, "bob" => 0; "tony" => 1; "mike" => 2).
  • password is then the password string at that same index (in your example,
    "pass" => 0; "pass2" =>1; "pass3" => 2).
  • Then the if compares the password associated with the user
    (password) with the password that was input in the dialog
    (input2).
陈独秀 2024-12-21 15:32:48

您必须搜索用户名表,找到引入的用户名的位置,然后检查该位置的密码是否等于用户引入的密码。

You'll have to search the username table, find the position of introduced username and then check if the password on that position is equal to what the user introduced.

傾城如夢未必闌珊 2024-12-21 15:32:48

您必须以某种方式在数组中搜索您要查找的字符串。有很多方法可以做到这一点,我将概述一种方法。

int index = 0;
for (String s : user) {
    if (s.equals(userin.getText()) {
        // the username matches! now check to see if the password at the _same index_ matches
        if (pass[index].eqals(input2.getText()) {
            // correct username and password!
        } else {
            // bad password!
        }
    }
    ++index;
}

我假设您有一个名为 pass 的密码数组,并且索引与 user 数组的索引匹配。 (user[i] 的密码是 pass[i]

You have to somehow search the array for the string you're looking for. There are a bunch of ways of doing this, I'll outline one method.

int index = 0;
for (String s : user) {
    if (s.equals(userin.getText()) {
        // the username matches! now check to see if the password at the _same index_ matches
        if (pass[index].eqals(input2.getText()) {
            // correct username and password!
        } else {
            // bad password!
        }
    }
    ++index;
}

I'm assuming that you have a password array called pass and that the indices match those of the user array. (user[i]'s password is pass[i])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文