Solved

Will not print my ArrayList

Posted on 2007-12-05
15
660 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 72
java 8 lambda expresssions exception handling 3 75
print map entry 34 56
diffSum example 4 27
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

861 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now