tags:

views:

73

answers:

4

Hi,

I am doing a password login that requires me to match two array: User and Pass. If user key in "mark" and "pass", it should show successfully. However I have trouble with the String[] input = pass.getPassword(); and the matching of the two arrays.

  String[] User = {"mark", "susan", "bobo"};

  String[] Pass = {"pass", "word", "password"};
  String[] input = pass.getPassword();

  if(Pass.length == input.length && user.getText().equals(User))
  {
     lblstat.setForeground(Color.GREEN);
     lblstat.setText("Successful");
  }
  else
  {
     lblstat.setForeground(Color.RED);
     lblstat.setText("Failed");
  }
A: 

How about Arrays.equals(input, Pass) ?

Maurice Perry
+3  A: 

I recommend to use a Map<K, V> instead. This way it's more easy to keep key-value pairs together.

Map<String, String> logins = new HashMap<String, String>();
logins.put("mark", "pass");
logins.put("susan", "word");
logins.put("bobo", "password");

String username = user.getText();
String password = pass.getPassword();

if (logins.containsKey(username) && logins.get(username).equals(password)) {
    // Known login.
} else {
    // Unknown login.
}

To learn more about maps, check the Sun tutorial on the subject.

BalusC
A: 

I didnt get your question completely but i can guess what you are looking for:

instead of array you can use hashmap with key as the user values and the value as the pass values. so when when u want a password of a particular user u can get that from the map.

GK
A: 

I suggest you use some sort of key-value data structure as well. You can also try this:

char[] input = pass.getPassword();
String password = new String(input);

then you do as BalusC suggested. You might consider trimming the string read from the text box containing the user name.

npinti