Java: Searching an array by a variable in an instance of object
<p>Overall goal is to create a library simulation that allows searches for books as well as transactions eg to borrow a book. I've only included the parts I think are relevant. </p>

<p>I have the array <code>stock</code> in the <code>Biblio</code> class as follows:   </p>

<pre><code>public class Biblio{
    private ArrayList&lt;Book&gt; stock;
</code></pre>

<p>and the <code>Book</code> class with the constructor:</p>

<pre><code>    public Book(String author , String title)
    {
        this.author = author;
        this.title = title ;
        code = null;//this is a String

    }
</code></pre>

<p>In the main method I am trying to search for a Book object using the <code>code</code> (which is <code>String</code>), I am using the <code>findCode</code> method in the <code>Biblio</code> class:</p>

<pre><code>    public Book findCode(String searchedCode){
        Book foundBook = null;
        for(Book bookObj : stock){

            if(bookObj.getCode().equxals(searchedCode)){
                 bookObj= foundBook;}//
        return foundBook;
    }
</code></pre>

<p>and here is the code in the main method I currently have:</p>

<pre><code> Biblio libSim;
 libSim = generateLib();                            
                                   ...
System.out.println(libSim.findCode(scan.next()));
</code></pre>

<p>but the output is coming up as null and I think it is because the <code>foundBook</code> equals null. However I can't return <code>bookObj</code>.</p>

<p>I do not know how I can find an object in an array by searching for the <code>code</code> variable. The books are added to the library in another method and using a setter method I have set any <code>code</code> that equals <code>null</code> to a string with the format <code>LIB0001</code>, this is done before I call the <code>findCode</code> method. </p>

<p><strong>How do you find an object by searching the array for a specific instance variable?</strong></p>