Solved

Will not print my ArrayList

Posted on 2007-12-05
15
670 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 45
null output 3 55
java imports not found 4 70
How to configure empty element in XML Document parser? 15 57
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

738 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