Results 1 to 10 of 10
  1. #1
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default Adding books into arrayList

    I am working on my assignment and I have managed to do most of the simple parts. Please help. I need to create a class that adds the following into an array


    public class LibraryTester {
    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"));


    I came up with the following

    public void addBook(Book bookObject) {
    myArr.add(bookObject);
    System.out.println(myArr);
    System.out.println(title);
    }



    Please note that this is just part of the code.what i need to know is to how to add the books into an arraylist or what ever object ca hold the books.
    Thank

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    not able to understand what actually you need.
    Mak
    (Living @ Virtual World)

  3. #3
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    I am trying to add 4 books into an array. To do this, I have to create a class that inputs the books into the array.

  4. #4
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    You cant use array.you can use arraylist only.
    Create book object look Book book=new Book("Book Name");
    after that add it into arraylist like

    ArrayList list=new ArrayList();
    list.add(book);

    hope this helps u.
    Mak
    (Living @ Virtual World)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    So, what error are you getting?
    Because if myArr is an ArrayList<Book> then myArr.add(bookObject) should work.

  6. #6
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    when I run the code I was getting iteration on each book added and it was null. No titles appeared when printed.
    the result looks something like this
    [Book@19821f]
    null
    [Book@19821f, Book@addbf1]
    null
    [Book@19821f, Book@addbf1, Book@42e816]
    null
    [Book@19821f, Book@addbf1, Book@42e816, Book@9304b1]

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Since we don't knwo what title is, or where title is populated, all we can say is that title is null.

    Your books are being added, as can be seen by the references printed out.

  8. #8
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    Let me post the entire code so that you can guide me. See where I have made mistake and help me complete the code.

    CODE:

    public class LibraryTest {
    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:");
    firstLibrary.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();

    }
    }


    From the above main method, I am to create a class that answers to the methods called in the main method. I came up with the code below and I need to complete it. Now I am stuck on the part where I have to add the books to the library. All I need is to be able to add the books to the library and I will manage the rest. However, when I get stuck againg I shall seek help.

    CODE:


    import java.util.*;
    public class Library {
    String address;
    String title;
    // String[] book = new String[4];
    int noOfBooks;
    //Book eBook = new Book();
    int date;
    String time;
    ArrayList<Book> myArr = new ArrayList<Book>();
    //ArrayList<String> BookList = new ArrayList();
    //ArrayList<Score> list = new ArrayList<Score>();

    public Library(String LibAddress) {
    address = LibAddress;
    // title = bookTitle;
    //Book = new ArrayList;
    }

    public void printOpeningHours() {
    //System.out.println("Library hours");
    System.out.println("Libraries are open daily from 9am to 5pm.");

    }

    public String getAddress(){
    return address;
    }

    public void printAddress() {
    System.out.println(address);
    }

    public String getTitle () {
    //if(title != null)
    return title;
    }
    public void addBook(Book bookObject) {
    myArr.add(bookObject);
    System.out.println(myArr);
    System.out.println(title);
    }



    public void borrowBook(String bookTitle){
    title = bookTitle;
    // if (title.equals(myArr(title)) {
    // }
    if(bookTitle != null) {
    myArr.remove(title); //System.out.println(" You have successfully borrowed." + title);
    } else if(bookTitle == null) {
    //myArr.add(title); //System.out.println ("sorry this book is not in the catalogue");
    }else{
    System.out.println("this book is already borrowed");
    }

    }

    void printAvailableBook(){

    }
    public void addBook(){
    ArrayList<String> Book = new ArrayList<String>();
    }
    public String getLibraryAdress(){
    return address;
    }
    public String getBookTitle(){
    return title;
    }

    }

    All the commenting out came up as I tried every possible solution that I came up with. Please ignore the irrelevant.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Use code tags please.
    I have already explained where you're going wrong.
    In that addBook method you are printing out title. Where is title set?
    Why does Library even have an attribute of title?

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    look at this tutorial and you will learn how to implement a library in a "state of the art" in java. if you need any help let me know.

Similar Threads

  1. Replies: 7
    Last Post: 09-26-2011, 11:29 PM
  2. Replies: 3
    Last Post: 11-05-2010, 10:31 AM
  3. adding date in arraylist
    By katturv in forum New To Java
    Replies: 1
    Last Post: 10-23-2010, 06:23 PM
  4. Null Pointer Exception when adding items to ArrayList
    By ShadowCopy in forum New To Java
    Replies: 3
    Last Post: 08-27-2009, 09:23 AM
  5. java books
    By doneritebob in forum New To Java
    Replies: 5
    Last Post: 01-20-2009, 02:41 AM

Posting Permissions

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