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?
4 Answers 4
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
}
}
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
).
-
Some commentary on what this is actually doing:
Arrays.asList(x)
converts an arrayx
into ajava.util.List
, with all of the elements of the list appearing in the same order that they appeared in in the array.indexOf
is a method onList
, which returns the first index where the given element is found, or-1
if the given element is not in the list. Note that the above code will actually crash with anArrayIndexOutOfBoundsException
if the given user name is not in theuser
array, because on line 2 you'd be indexing into thepass
array with-1
.Mike Daniels– Mike Daniels2011年11月09日 01:39:33 +00:00Commented Nov 9, 2011 at 1:39
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.
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]
)
Collection
classes available in packagejava.util
instead of an array of strings.Collection
has acontains
method that can check for the existence of a particular item in the collection.