Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Working with an ArrayList

Mitchell McKnight
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help. Could some one please help me.

Create a Book class that stores book information. Your book class will store book title, author's first name, author's last name, book ISBN-10 number, and publisher, and price.
Create a test application that creates several objects of type book class and add each book to an arraylist. Your test application should then do the following:

1. iterate through all books in the arrayList and print the information out in readable form.
2. Print the total number of books stored.
3. Prompt you for an index and print the book information stored in the index.

i am getting this error when i hit run:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(
at java.util.Arrays.copyOf(
at java.util.ArrayList.ensureCapacity(
at java.util.ArrayList.add(
at BookList.BookInfo(
at BookTest.main(
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)

here is what i have:

public class Book
private String bookTitle;
private String authorsFirstName;
private String authorsLastName;
private int iSBNNumber;
private String publisher;
private double price;

public Book()


public Book(String title,String first,String last, int num,String pub,double pric)
setBookTitle (title);
setAuthorsFirstName (first);
setAuthorsLastName (last);
setISBNNumber (num);
setPublisher (pub);
setPrice (pric);

public void setBookTitle (String title)
bookTitle = title;
}//MTM:End of setTitle

public String getBookTitle()
return bookTitle;

}//MTM:End of getTitle
public void setAuthorsFirstName (String first)
authorsFirstName = first;

}//MTM:End of setFristName
public String getAuthorsfirstName()
return authorsFirstName;
}//MTM:End of getAuthorsFirstName

public void setAuthorsLastName (String last)
authorsLastName = last;

}//MTM:End of setAuthorsLastName
public String getAuthorsLastName()
return authorsLastName;

}//MTM:End of getAuthorsLastName
public void setISBNNumber (int num)
iSBNNumber = num;

}//MTM:End of setISBNNumber
public int getISBNNumber()
return iSBNNumber;

}//MTM:End of getISBNNumber
public void setPublisher (String pub)
publisher = pub;

}//MTM:End of setPublisher
public String getPublisher()
return publisher;

}//MTM:End of getpublisher
public void setPrice (double pric)
price = pric;

}//MTM:End of setPrice
public double getPrice()
return price;
}//MTM:End of getPrice
}//MTM: End of Class Book


import java.util.*;

public class BookList
//create scanner, array list, and book record object

public void BookInfo()
Scanner input = new Scanner(new File("src/books.txt"));

ArrayList aList = new ArrayList();
//start while loop to create set information
Book book = new Book();

//add book to array list

}//end while
//print book list
int count = 0;
while(count < aList.size())
String temp = aList.get(count).toString();

}//end while

}//end try
catch(FileNotFoundException fileNotFoundException)
System.err.println("File not found.");
}//end catch
catch(SecurityException securityException){
System.err.println("You don't have permission to access the file.");
}//end catch
catch(NoSuchElementException noSuchElement)
System.err.println("No Such Element.");
}//end catch
}//end cardCatalog method

}//end class bookList

public class BookTest
public static void main (String arg [])
BookList application =new BookList();


This is my text file:

UNIXBasics James Sampson 125313 HeathPress 30.00
JavaNOW Frank Johnson 273456 RiversidePress 45.00
C_programing John Izel 777246 ProgrammerPress 52.00
Office2007 Bill Gates 345210 MicrosoftPress 60.00
Networking Mitchell McKnight 777777 TimeoutPress 49.99
Paul Clapham
Posts: 21443
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are running out of memory (that's what the error message says, right?) then you must be creating an extremely large number of Book objects. You should find out why that is happening.

And by the way, it makes it much easier if you post your code inside the "code" tags. Notice the little "code" button above the posting box which allows you to do that. Quite often people won't read large blocks of unformatted code -- I didn't, for example.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic