?
Solved

Will not print my ArrayList

Posted on 2007-12-05
15
Medium Priority
?
673 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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