Solved

Will not print my ArrayList

Posted on 2007-12-05
15
664 Views
Last Modified: 2010-04-21
i get memory index not the arraylist elements?
my Book ListBook@1ad086a
my Book ListBook@10385c1
my Book ListBook@42719c
my Book ListBook@30c221
my Book ListBook@119298d
my Book ListBook@f72617
my Book ListBook@1e5e2c3
my Book ListBook@18a992f
my Book ListBook@4f1d0d
/**********************************************************
*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
 
		//Make a bookslist
 
        	books.add(new Book("Band of Brothers", "Ambrose", "Stephen E.", 10, 9.60));
        	books.add(new Book("Beloved", "Morrison","Toni", 23, 12.95));
        	books.add(new Book("Catch-22","Heller","Joseph",12, 12.00));
        	books.add(new Book("East of Eden","Steinbeck","John",45, 12.95));
        	books.add(new Book("Harry Potter and the Goblet of Fire","Rowling","J.K.",2, 18.16));
        	books.add(new Book("Of Mice and Men","Steinbeck","John",13,6.95));
        	books.add(new Book("The Catcher in the Rye","Salinger","J.D.",11, 5.99));
        	books.add(new Book("The Grapes of Wrath","Steinbeck", "John", 17, 13.00));
        	books.add(new Book("To Kill a Mockingbird","Lee","Harper",8, 18.00));
 
        	for (int i=0; i<books.size(); i++){
				System.out.println("my Book List"+ books.get(i));
 
			}// end for i loop
 
	  	// 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
  • 9
  • 3
  • 3
15 Comments
 
LVL 9

Expert Comment

by:william007
ID: 20413651
Hi, something wrong in this line
System.out.println("my Book List"+ books.get(i));

You should get the book object first, and call the method or properties inside to get the value, eg
Book book=books.get(i);
System.out.println("my Book List"+ book.name);

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20413751
Book needs to implement toString to print itself properly
0
 
LVL 9

Expert Comment

by:william007
ID: 20413784
Yea, this is another possibility, if the format is predefined :)
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:vcaces
ID: 20414876
output is still not showing my data:

my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
my Book List[Book@10385c1, Book@42719c, Book@30c221, Book@119298d, Book@f72617,
Book@1e5e2c3, Book@18a992f, Book@4f1d0d, Book@1fc4bec]
/**********************************************************
*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
 
		//Make a bookslist
 
        	books.add(new Book("Band of Brothers", "Ambrose", "Stephen E.", 10, 9.60));
        	books.add(new Book("Beloved", "Morrison","Toni", 23, 12.95));
        	books.add(new Book("Catch-22","Heller","Joseph",12, 12.00));
        	books.add(new Book("East of Eden","Steinbeck","John",45, 12.95));
        	books.add(new Book("Harry Potter and the Goblet of Fire","Rowling","J.K.",2, 18.16));
        	books.add(new Book("Of Mice and Men","Steinbeck","John",13,6.95));
        	books.add(new Book("The Catcher in the Rye","Salinger","J.D.",11, 5.99));
        	books.add(new Book("The Grapes of Wrath","Steinbeck", "John", 17, 13.00));
        	books.add(new Book("To Kill a Mockingbird","Lee","Harper",8, 18.00));
 
        	for (int i=0; i<books.size(); i++){
				Book book=books.get(i);
				System.out.println("my Book List"+ books.toString());
 
			}// end for i loop
 
	  	// 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]);
			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
 
LVL 9

Expert Comment

by:william007
ID: 20415016
Hi, add the following code
 public String toString(){
     return getTitle();
 }
to Book.java

and change the following code in BookDriver.java

for (int i=0; i<books.size(); i++){
                Book book=books.get(i);
                System.out.println("my Book List"+ books.toString());

}// end for i loop

to

System.out.println("my Book List"+ books.toString());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20415047
>>output is still not showing my data:

That's because you didn't follow my advice of implementing Book.toString
0
 
LVL 9

Accepted Solution

by:
william007 earned 500 total points
ID: 20415111
However, if you do not want to change your Book.java for any reason(like add in a toString method as mentioned by CEHJ), you can simply print it out, following is the second method(Not sure how detail you need....just for your reference purpose):


Change
for (int i=0; i<books.size(); i++){
                Book book=books.get(i);
                System.out.println("my Book List"+ books.toString());

}// end for i loop

to

System.out.println("===my Book List===");
for (int i=0; i<books.size(); i++){
    Book book=books.get(i);
    System.out.println((i+1)+")");
    System.out.println("Title= "+book.getTitle());
    System.out.println("Author First Name= "+book.getAuthorFirstName());
    System.out.println("Author Last Name= "+book.getAuthorLastName());
    System.out.println("Price= "+book.getPrice());
    System.out.println("Quantity= "+book.getQuantity());    
    System.out.println();
}// end for i loop
System.out.println("===my Book List Ended===");
0
 

Author Closing Comment

by:vcaces
ID: 31412915
That work Super now I understand why Arraylist is so like.
0
 
LVL 9

Expert Comment

by:william007
ID: 20415287
Glad that helps :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20415304
That will of course work, but is an inferior and unreusable way of solving the problem
0
 
LVL 9

Expert Comment

by:william007
ID: 20415348
Hi, actually, the write up of second method is due to I see the following sentence
"The Class that can not be change"
in this url
http://www.experts-exchange.com/Programming/Languages/Java/Q_23003330.html
just above the book class.
0
 

Author Comment

by:vcaces
ID: 20415383
That is right I do not want to change the Book.java class.
What is the best way to sort by the AuthorLastName?

Thx William007
0
 
LVL 9

Expert Comment

by:william007
ID: 20415619
Java has an api for sorting which is Collection.sort...but since you can't edit your book class to implements the comparable interface...an alternative way is, to use some simple sorting algorithms like bubble sort to sort by comparing the AuthorLastName.

If efficiency is a concern, can consider mergesort or quicksort, but that will complex the code a bit.
0
 
LVL 9

Expert Comment

by:william007
ID: 20415716
But actually there is an more easy way, which is to input the comparable class to the Collection.Sort second arguments. Forget about those sorting algorithms, since rely on java sorting algorithms is much more better :)
0
 
LVL 9

Expert Comment

by:william007
ID: 20415743
Typo, not comparable is Comparator..
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 55
login jsp example 24 46
throw exception 21 42
Android development question 2 34
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

805 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