Welcome to the Ranch
Unfortunately an array cannot compare input to its contents. You need outside code to do it, and I am afraid you haven't found a very good resource. The code formatting in that link is poor. I am pleased to see corrected some of the formatting, but the comments in that link aren't useful. For example that on their line 32 says,
//check if string array contains the string
which is incorrect. I presume you need to use array because there are more sophisticated data structures which actually do check whether they contain a particular element.
You appear to be implementing a linear search, which is quite a simple algorithm. Note that a search usually returns the index at which the element is found or
−1 is it isn't found. You can
test whether the loop needs to run by testing for
contains or the value of the index.
I presume your
else isn't reached because you always find the object you are seeking. You may be able to shorten the code to print results with the
?: operator.
You are right to test for
nulls (your line 7) but as long as your input isn't
null, you can reverse the objects for the
equals() test and not worry about
nulls.
I am not happy about adding a new object in the same
else. I haven't got the time to say anything else now: sorry.