0

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");
  }
1
  • be more specific than "I have trouble". Are you getting error messages? Output? What exactly is the issue? At first glance it looks like you're comparing a string to an array of strings with your getText().equals command Commented Apr 13, 2010 at 18:49

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

How about Arrays.equals(input, Pass) ?

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.