Java, how to compare Strings with String Arrays
If I understand your question correctly, it appears you want to know the following:
How do I check if my
String
array containsusercode
, theString
that was just inputted?
See here for a similar question. It quotes solutions that have been pointed out by previous answers. I hope this helps.
I presume you are wanting to check if the array contains a certain value, yes? If so, use the contains
method.
if(Arrays.asList(codes).contains(userCode))
Iterate over the codes
array using a loop, asking for each of the elements if it's equals()
to usercode
. If one element is equal, you can stop and handle that case. If none of the elements is equal to usercode
, then do the appropriate to handle that case. In pseudocode:
found = false
foreach element in array:
if element.equals(usercode):
found = true
break
if found:
print "I found it!"
else:
print "I didn't find it"
Right now you seem to be saying 'does this array of strings equal this string', which of course it never would.
Perhaps you should think about iterating through your array of strings with a loop, and checking each to see if they are equals() with the inputted string?
...or do I misunderstand your question?