将字符串与字符串数组进行比较
基本上我想做的是一个非常简单的登录屏幕来了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这可能对您有帮助,因为我认为您必须在这个数组中检查每个用户的密码:
i think this may help you,because i think you have to check each user with his password int this array:
假设您有一个
pass
数组来匹配您的user
数组,并且user
中的每个条目都保证在pass 中具有相应的条目
,那么以下解决方案应该有效:Arrays.asList(user).indexOf(userin.getText())
将获取以下索引列表中的用户(在您的示例中,“bob”=> 0;“tony”=> 1;“mike”=> 2)。
password
是同一索引处的密码字符串(在您的示例中,“通过”=> 0; “pass2”=>1; “pass3” => 2)。
(
password
) 与在对话框中输入的密码(
输入2
)。Assuming you have a
pass
array to match youruser
array, and every entry inuser
is guaranteed to have a corresponding entry inpass
, then the following solution should work:Arrays.asList(user).indexOf(userin.getText())
will get the index ofthe 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).
(
password
) with the password that was input in the dialog(
input2
).您必须搜索用户名表,找到引入的用户名的位置,然后检查该位置的密码是否等于用户引入的密码。
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.
您必须以某种方式在数组中搜索您要查找的字符串。有很多方法可以做到这一点,我将概述一种方法。
我假设您有一个名为
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.
I'm assuming that you have a password array called
pass
and that the indices match those of theuser
array. (user[i]
's password ispass[i]
)