aspose file tools*
The moose likes Beginning Java and the fly likes How can I retrive the Books a Person has borrowed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I retrive the Books a Person has borrowed?" Watch "How can I retrive the Books a Person has borrowed?" New topic
Author

How can I retrive the Books a Person has borrowed?

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167

I am using setBooks() and getBooks() methods,setBooks() adds the Book objects to the bookList ArryList passed to it,getBooks() returns the bookList ArrayList<Book>.By using these two methods how can I retrive the Books a pariticular person has borrowed.when I try to do it using a for each loop
p1 is a Person object

for(Person aPerson: p1.getBooks())
{
if(aPerson.getBooks !=null) &&
aPerson.getBooks. This is where the problem for me the arryList objects does not contain the getTitle in the Book class
}


What is the path to take?


Varuna Seneviratna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39419
    
  28
You are trying to iterate a List and telling it to find Person objects. But the List contains Book objects.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Originally posted by Campbell Ritchie:
You are trying to iterate a List and telling it to find Person objects. But the List contains Book objects.


No! I am trying to find Book objects which are contained in a Person Object.
The Person class has a getBooks() which returns a <Book>type ArrayList.By creating a p1 Person type object and setting the book objects borrowed by p1 by calling setBooks(), I want to add the books which were borrowed by the person p1 to another ArrayList(setBooks(Book b1) adds the passed Book objects to an ArryList).Then creating a for each loop I want to iterate through all the Person objects p1,p2,p3 and calling the getBooks() of each Person object which returns a ArrayList<Book> and collect all the book objects borrowed by a Particular person.the p1 in the for each loop has to be changing in order to include all the Person objects.

for(Person aPerson: p1.getBooks())
{
if(aPerson.getBooks !=null) &&
aPerson.getBooks. This is where the problem for me the arryList objects does not contain the getTitle in the Book class
}

How can I solve this?

Varuna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39419
    
  28
Originally posted by myself:
You are trying to iterate a List and telling it to find Person objects. But the List contains Book objects.


Yes, you are. You have put p1.getBooks() to the right of a : in a for-each loop, and Person aPerson to the left of the :. The getBooks() method returns a List<Book>, so you are in fact trying to get Person objects out of a List of Books.
This is followed by a call to getBooks without the () and a null check.

Those first and second points both constitute compiler errors.

Also the null check is unnecessary; if you had encountered a null by now, you would have suffered a NullPointerException before that line.

When you are inside the for-each loop, you don't have an ArrayList object, you should have Book objects. So your problem about getTitle() will be sorted out when you get the for-each loop right.

You need to find out how to write a for-each loop; you should find details in the Java Tutorials. Remember if what is on the right of the : is supposed to contain books, whatever is on the left of the : will be Books. A for-each loop is also called an enhanced for loop.

There are also methods in the List interface which allow you to add the entire contents of another List. You may find it easier to use them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I retrive the Books a Person has borrowed?