-2

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.

I have the array stock in the Biblio class as follows:

public class Biblio{
    private ArrayList<Book> stock;

and the Book class with the constructor:

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

    }

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

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

            if(bookObj.getCode().equxals(searchedCode)){
                 bookObj= foundBook;}//
        return foundBook;
    }

and here is the code in the main method I currently have:

 Biblio libSim;
 libSim = generateLib();                            
                                   ...
System.out.println(libSim.findCode(scan.next()));

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

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

How do you find an object by searching the array for a specific instance variable?

Maitiu
  • 118
  • 2
  • 10

1 Answers1

0

You cannot compare Strings with ==

see here on how to do it properly: How do I compare strings in Java?

Community
  • 1
  • 1
Mate Hegedus
  • 2,887
  • 1
  • 19
  • 30
  • Didn't know it wasn't possible. However I did originally have `.equals()`, but its up the warning 'object equals null is never true', – Maitiu Apr 09 '16 at 21:15
  • You have to use equals because String is an object. It is different than int or double. – Mate Hegedus Apr 09 '16 at 21:16
  • I have .equals() originally but this never worked for me either, the question isn't how do I compare strings, it is how do I find an object in an array by comparing instance variables of the objects? – Maitiu Apr 09 '16 at 21:19
  • Your algorithm should work if you use equals(). Your algorithm is a simple for loop. If you use equals() it should work absolutely fine. – Mate Hegedus Apr 09 '16 at 21:42
  • It doesn't. I am getting null pointer errors and have no idea why. – Maitiu Apr 09 '16 at 21:47
  • I think I see your problem. 1) use equals() and 2) in the for loop you say bookObj = foundBook when it should be "foundBook = bookObj" – Mate Hegedus Apr 09 '16 at 22:01
  • 1) fixed, 2) fixed. However the null pointer issue seems to be with `if(selectedBook.getCode().equals(null)){`, apparently the object equals null is never true, but when assigining values to `code`, only some objects are assigned values, so there should be cases of `code=null` – Maitiu Apr 09 '16 at 22:09