Results 1 to 10 of 10
Thread: Adding books into arrayList
- 12-15-2010, 12:48 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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
- 12-15-2010, 10:21 AM #2
not able to understand what actually you need.
Mak
(Living @ Virtual World)
- 12-15-2010, 11:06 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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.
- 12-15-2010, 11:23 AM #4
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)
- 12-15-2010, 11:36 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
So, what error are you getting?
Because if myArr is an ArrayList<Book> then myArr.add(bookObject) should work.
- 12-15-2010, 12:19 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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]
- 12-15-2010, 12:24 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 12-15-2010, 01:26 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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.
- 12-15-2010, 01:33 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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?
- 12-15-2010, 02:10 PM #10
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
-
adding values from file to multi-dimensional ArrayList
By sebo in forum New To JavaReplies: 7Last Post: 09-26-2011, 11:29 PM -
question about adding using ArrayList using terminal
By chmo in forum New To JavaReplies: 3Last Post: 11-05-2010, 10:31 AM -
adding date in arraylist
By katturv in forum New To JavaReplies: 1Last Post: 10-23-2010, 06:23 PM -
Null Pointer Exception when adding items to ArrayList
By ShadowCopy in forum New To JavaReplies: 3Last Post: 08-27-2009, 09:23 AM -
java books
By doneritebob in forum New To JavaReplies: 5Last Post: 01-20-2009, 02:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks