Results 1 to 9 of 9
  1. #1
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Problems With ArrayLists

    Hello all,
    I am working with an arraylist for the first time and I am having a little bit of trouble. In my method where I am trying to print the contents of the arraylist it only prints the path. Any help is appreciated.

    Java Code:
    import book.pkg.*;
    import java.util.ArrayList;
    
    public class Library {
    	ArrayList<Book> library1 = new ArrayList<Book>();
    	String firstLibrary;
    
    	// Constructors for two libraries
    	Library(String address1) {
    		firstLibrary = address1;
    	}
    
    	// Add certain books to library arrays
    	public void addBook(Book book) {
    		library1.add(book);
    		book.getTitle();
    		
    	}
    
    	public static void printOpeningHours() {
    		System.out.println("Libraries are open daily from 9 AM to 5 PM");
    	}
    
    	public void printAddress() {
    		System.out.println(firstLibrary);
    	}
    
    	// Remove certain books from libraries
    	public void borrowBook(String book) {
    		//library1.remove(book);
    		//book.getTitle();
    		
    	}
    
    	// Print books in Libraries
    	public void printAvailableBooks() {
    		for(int i = 0; i <  library1.size(); i++){
    			System.out.println(library1.get(i));
    		}
    	}
    The method above is the method that prints the statement

    Java Code:
    // Re-add Borrowed books
    	public String returnBook(String Book) {
    		return Book;
    	}
    
    	public static void main(String[] args) {
    
    		// Create two libraries
    		Library firstLibrary = new Library("10 Main St.");
    		Library secondLibrary = new Library("228 Liberty St.");
    		// Add four books to the first library
    		firstLibrary.addBook(new Book("The Da Vinci Code"));
    		firstLibrary.addBook(new Book("Le Petit Prince"));
    		firstLibrary.addBook(new Book("A Tale of Two Cities"));
    		firstLibrary.addBook(new Book("The Lord of the Rings"));
    		// Print opening hours and the addresses
    		System.out.println("Library hours:");
    		printOpeningHours();
    		System.out.println();
    		System.out.println("Library addresses:");
    		firstLibrary.printAddress();
    		secondLibrary.printAddress();
    		System.out.println();
    		// Try to borrow The Lords of the Rings from both libraries
    		System.out.println("Borrowing The Lord of the Rings:");
    		firstLibrary.borrowBook("The Lord of the Rings");
    		firstLibrary.borrowBook("The Lord of the Rings");
    		secondLibrary.borrowBook("The Lord of the Rings");
    		System.out.println();
    		// Print the titles of all available books from both libraries
    		System.out.println("Books available in the first library:");
    		firstLibrary.printAvailableBooks();
    		System.out.println();
    		System.out.println("Books available in the second library:");
    		secondLibrary.printAvailableBooks();
    		System.out.println();
    		// Return The Lords of the Rings to the first library
    		System.out.println("Returning The Lord of the Rings:");
    		firstLibrary.returnBook("The Lord of the Rings");
    		System.out.println();
    		// Print the titles of available from the first library
    		System.out.println("Books available in the first library:");
    		firstLibrary.printAvailableBooks();
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problems With ArrayLists

    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()?

  3. #3
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default 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.

  4. #4
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Problems With ArrayLists

    What else do you want it to say?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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
    Java Code:
    public class Book {
        // existing code here
    
        @Override
        public String toString() {
            return "the string you want the book to display: title etc";
        }
    }

  6. #6
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Re: Problems With ArrayLists

    Quote Originally Posted by JavaAdviser View Post
    What else do you want it to say?
    I want it to print out the books in arraylist: library1

  7. #7
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Re: Problems With ArrayLists

    Quote Originally Posted by pbrockway2 View Post
    Java 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?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

  9. #9
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Re: Problems With ArrayLists

    I was trying to print out the title, but I got it figured out I appreciate your time and effort.
    Last edited by philip1597; 09-18-2012 at 02:14 AM.

Similar Threads

  1. I don't get ArrayLists
    By Gio!? in forum New To Java
    Replies: 10
    Last Post: 05-03-2012, 01:50 PM
  2. Arraylists
    By talia in forum New To Java
    Replies: 5
    Last Post: 01-30-2012, 07:44 PM
  3. help with ArrayLists !
    By bigbreez21 in forum New To Java
    Replies: 3
    Last Post: 11-20-2011, 09:23 PM
  4. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 05:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •