Re: Problems With ArrayLists
Quote:
In my method where I am trying to print the contents of the arraylist it only prints the path.
I'm not really sure what you mean by the "path".
Do you mean the printAvailableBooks() method is not doing what you want? If so, what is printed when you call firstLibrary.printAvailableBooks() in main()?
Re: Problems With ArrayLists
Yes the method printAvailableBooks is not doing what i want it to do. It prints "book.pkg.Book@1f1fba0
book.pkg.Book@1befab0
book.pkg.Book@13c5982
book.pkg.Book@1186fab"
I'm pretty sure that that is the path to the book method that it is printing instead of the actual contents which doesn't make sense to me.
Re: Problems With ArrayLists
What else do you want it to say?
Re: Problems With ArrayLists
Those funny lines are identifiers for the book instances.
In order to have them print in a more readable way you should add a toString() method to the Book class
Code:
public class Book {
// existing code here
@Override
public String toString() {
return "the string you want the book to display: title etc";
}
}
Re: Problems With ArrayLists
Quote:
Originally Posted by
JavaAdviser
What else do you want it to say?
I want it to print out the books in arraylist: library1
Re: Problems With ArrayLists
Quote:
Originally Posted by
pbrockway2
Code:
public class Book {
// existing code here
@Override
public String toString() {
return "the string you want the book to display: title etc";
}
}
Would that be a replacement for PrintAvailableBooks()? or call to PrintAvailablebooks()? and
out of curiousity what is the @Override for?
Re: Problems With ArrayLists
It has nothing to do with the (or any) library, including the printAvailableBooks() method.
Earlier you said "I want it to print out the books in arraylist: library1". But programming is all about precision: you didn't mean you wanted to print the contents of the books, did you? That's why (I'm guessing) JavaAdviser asked what you wanted to be printed. And we still don't really know. We haven't seen this Book class, so we don't know what sorts of characteristics Book instances have (title, id number, author, date, etc) and we don't really know what it is you want printed.
Anyway, the toString() method goes into the Book class. (Try copy and pasting it exactly as I posted it and see what happens!). The intention of the method is to say exactly what should be printed when you print a specific book. It can return any string at all. (eg title and author). And that string will be used whenever System.out.println() prints the book.
@Override is there because this toString() method is actually defined in the Object class. Your Book class is a subclass of Object and, at the moment, System.out.println() is using the Object toString() method to figure out what to print. (And, as you've found, that's not the best.) When we intend to redefine a method like this it is usual to add @Override to tell the compiler what we're doing. (The redefinition of the method is called "overriding"). It's not necessary, but it can catch typos and it documents things nicely.
Re: Problems With ArrayLists
I was trying to print out the title, but I got it figured out I appreciate your time and effort.