?
Solved

can not ArrayList to add

Posted on 2007-12-05
5
Medium Priority
?
557 Views
Last Modified: 2011-09-20
Why will my code not compile error with the add to arraylist.
The Class that can not be chage 
 
***********************************************************************/
public class Book
{
 
  private String title;				// Book title
  private String authorLastName;    // Book author's last name
  private String authorFirstName;   // Book author's first name
  private int    quantity;    		// Books in stock
  private double  price; 			// Book price
 
//**********************************************************
 
  // Constructor
 
  public Book(String title, String lastName, String firstName, int quantity, double price) {
    this.setTitle(title);
    this.setAuthorLastName(lastName);
    this.setAuthorFirstName(firstName);
    this.setQuantity(quantity);
    this.setPrice(price);
  } // end constructor
 
//**********************************************************
 
  // Accesors
 
  public String getTitle() {
    return this.title;
  } // end getTitle
 
  public String getAuthorLastName() {
    return this.authorLastName;
  } // end getAuthorLastName
 
  public String getAuthorFirstName() {
    return this.authorFirstName;
  } // end getAuthorFirstName
 
  public int getQuantity() {
    return this.quantity;
  } // end getQuantity
 
  public double getPrice() {
    return this.price;
  } // end getPrice
 
//**********************************************************
 
  // Mutators
 
  public void setTitle(String title) {
    this.title = title;
  } // end setTitle
 
  public void setAuthorLastName(String lastName) {
    this.authorLastName = lastName;
  } // end setAuthorLastName
 
  public void setAuthorFirstName(String firstName) {
    this.authorFirstName = firstName;
  } // end setAuthorFirstName
 
  public void setQuantity(int quantity) {
    this.quantity = quantity;
  } // end setPrice
 
  public void setPrice(double price) {
    this.price = price;
  } // end setPrice
 
  //*********************************************************
 
} // end class Book
 
########################################################################
Class Driver
/**********************************************************
*Author: Joshua.vangulden
*File Name: BookDriver.java
*CS219 HW#4
*
*This program is to help add new books, Author name and
*the price.
**********************************************************/
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
 
public class BookDriver {
  public static void main(String[] args) {
 
/*************raw data for input**************/
 
		String [] tittle = {"Band of Brothers", "Beloved", "Catch-22", "East of Eden",
							"Harry Potter and the Goblet of fire", "Of Mice and Men",
							"The Catcher in the Rye", "The Grapes Of Wrath",
							"To Kill a Mockingbird"}; //name of book
		String [] lname = {"Ambrose", "Morrison", "Heller", "Steinbeck", "Rowling",
						"Steinbeck", "Salinger", "Steinbeck", "Lee"} ; // Author Last Name
		String [] fname = {"Stephen E.", "Toni", "Joseph", "John", "J.K.", "John", "J.D.",
						"John", "Harper"} ; // Author first Name
		int [] qty = {10, 23, 12, 45, 2, 13, 11, 17, 8} ; // numbers of books in store
		double [] price = {9.60, 12.95, 12.00, 12.95, 18.16, 6.95, 5.99, 13.00, 18.00}; // price of each book
		//Book books;
 
/******* Sort the book by Author last name ********/
 
        ArrayList <Book> books = new ArrayList <Book>();//Array list of Book lists
 
        books.add("Band of Brothers", "Ambrose", "Stephen E.", 10, 9.60);
 
 
	  	// Arrays.sort();
 
 
 
 
 
 
/******** print to screen ************/
 
		System.out.println ("                           Book Report. Sorted by author");
		System.out.println (" ------------------------------------------------------------------------------");
		System.out.println ("	Title				Last Name   First Name    Qty     Price");
		System.out.println (" ------------------------------------------------------------------------------");
 
		for(int i=0; i<qty.length; i++){
			System.out.printf("%-40s%-12s%-12s%5d%10.2f\n", tittle[i], lname[i], fname[i], qty[i], price[i]);
		}// end i for loop
 
		System.out.println();
 
  } // end main
} // end class SalesManagerDriver

Open in new window

0
Comment
Question by:vcaces
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 20412367
>>books.add("Band of Brothers", "Ambrose", "Stephen E.", 10, 9.60);
 
should probably be
books.add(new Book("Band of Brothers", "Ambrose", "Stephen E.", 10, 9.60));
 

Open in new window

0
 
LVL 11

Expert Comment

by:raj3060
ID: 20412613
CEHJ is corect:

this should be changed:

books.add(new Book("Band of Brothers", "Ambrose", "Stephen E.", 10, 9.60));

You should be creating the new bean before you add it to ArrayList.
0
 

Author Closing Comment

by:vcaces
ID: 31412875
thx I be up all night trying to get this to work
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20413835
:-)

0
 

Author Comment

by:vcaces
ID: 20414913
that work grate thx but what is bean?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question