• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 691
  • Last Modified:

Will not print my ArrayList

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
vcaces
Asked:
vcaces
  • 9
  • 3
  • 3
1 Solution
 
william007Commented:
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
 
CEHJCommented:
Book needs to implement toString to print itself properly
0
 
william007Commented:
Yea, this is another possibility, if the format is predefined :)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
vcacesAuthor Commented:
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
 
william007Commented:
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
 
CEHJCommented:
>>output is still not showing my data:

That's because you didn't follow my advice of implementing Book.toString
0
 
william007Commented:
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
 
vcacesAuthor Commented:
That work Super now I understand why Arraylist is so like.
0
 
william007Commented:
Glad that helps :)
0
 
CEHJCommented:
That will of course work, but is an inferior and unreusable way of solving the problem
0
 
william007Commented:
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
 
vcacesAuthor Commented:
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
 
william007Commented:
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
 
william007Commented:
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
 
william007Commented:
Typo, not comparable is Comparator..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 9
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now