.containsAll will work but .equals is not working.
-
Apparently, digitList and passcode are both List
instances. So there's no way that digitList.contains(passcode)) ever returns true, because a list of integers can't possibly contain a list. It only contains integers. Add some System.out.println() instructions to print what your two lists are, or use your debugger. – JB Nizet May 22 '17 at 18:39 -
Add `passcode.clear();` in when you clear digitList – Josh Heaps May 22 '17 at 18:44
-
Stackoverflow is getting into a nasty habit of downvoting questions. Why? I'm getting afraid to post anymore. – jiveturkey May 22 '17 at 18:47
-
@jiveturkey Because badge? – Tezra May 22 '17 at 18:53
-
1@jiveturkey I'm not a downvoter here, but it's not a very good question. It is not a complete program, and we don't even get to see the declaration and instantiation of one of the main players (`digitList`). It is certainly not an [mcve](https://stackoverflow.com/help/mcve), which for a debugging question of this size is what I'd require before considering the upvote button. – yshavit May 22 '17 at 19:00
-
Ok, maybe this wasn't the best example for me vent on. – jiveturkey May 22 '17 at 19:01
3 Answers
You are adding the numbers everytime. You should use local variables or local initialization. Else your passcode list gets 5 more numbers everytime you call your function.

- 1,626
- 16
- 26
Because you said "a security keypad", I have to say ignore what anyone else says that is technically correct, you are doing it wrong. By storing the passcode in a "plain" format, you might as well just leave a sticky note on the keypad with the passcode on it.
What you should be doing is hashing the input and the passcode and comparing the hashed versions. And don't use hashCode(), that function is unreliable for this purpose. (Example of how to hash)
Also, according to the Java Docs, equals is the correct way to check. You are miss-handling your list instances. You should step through the debugger to see everywhere in your code that you do something that alters your global variables.

- 8,463
- 3
- 31
- 68
I think you have a problem where you are comparing your arrays. Have a look at this SO question: equals vs Arrays.equals in Java
if (digitList.size() == 5 && digitList.equals(passcode)) { // are the arrays the same array?
guideArea.setText("Correct Password.");
digitList.clear();
}
what it should be:
if (digitList.size() == 5 && Arrays.equals(digitList, passcode)) { // are the two arrays CONTENT the same
guideArea.setText("Correct Password.");
digitList.clear();
}

- 802
- 7
- 12