• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How can I retrive the Books a Person has borrowed?

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79968
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to iterate a List and telling it to find Person objects. But the List contains Book objects.
 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79968
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 AM MIGHTY! Especially when I hold this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic