wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Print Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Print Array" Watch "Print Array" New topic
Author

Print Array

Patty Kingsmill
Greenhorn

Joined: Nov 20, 2005
Posts: 7
I have the following code:

public class DTTMCatalog
{ //open class DTTMCatalog
private catalogItems items[]; //array for items stored in the DTTM catalog
public DTTMCatalog()
{
items = new catalogItems[15];
items[0]= new catalogItems("song", "Hannah Jane");
items[1]= new catalogItems("song", "Hard Day's Night");
items[2]= new catalogItems("song", "Crazy");
}

public void getCatalog()
{
System.out.print ("Here are the items in the DTTM Catalog: \n");
System.out.printf("%s%8s%8s\n","Index", "Type", "Title");
for(int counter = 0; counter < 3; counter++)
System.out.printf("%15s%28s\n", counter, items[counter]);
}
}

public class catalogItems
{
private String itemType;
private String title;

public catalogItems(String theItemType, String theTitle)
{
itemType = theItemType;
title = theTitle;
}

}

The problem is that my print output looks like this:

1 catalogItems@117a8bd

How do I get it to print the contents of the array rather than the hashcode?

Thanks
Patty
Jody Brown
Ranch Hand

Joined: Nov 09, 2005
Posts: 43
The hashcode you are seeing is the hashcode for each catalogItems object in each of the arrays. Instead, you should think about getting a handle to each of these objects inside your for loop, and doing a couple of System.outPrintln statements, calling the getters on them (once you write them of course ;-) ) to print the details you require. FYI, for your own sanity in debugging, class names should start with a capital letter, to distinguish them from method names.
[ November 20, 2005: Message edited by: Jody Brown ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37897
    
  22
Or give each of your items a proper toString() method.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

First of all, you implement a toString() method in your class catalogItems.
Here's a trick to get a nicely printed array:

[ November 21, 2005: Message edited by: Jesper de Jong ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Handy class, that java.util.Arrays. And if you are using the current version of
Java, you can even shorten that by one step:


There is no emoticon for what I am feeling!
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Jesper de Jong:
First of all, you implement a toString() method in your class catalogItems.
Here's a trick to get a nicely printed array:


[ November 21, 2005: Message edited by: Jesper de Jong ]


This is not performance-friendly, you can use org.apache.commons.lang.ArrayUtils.toString().


http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/ArrayUtils.html


ASCII silly question, Get a silly ANSI.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Print Array
 
Similar Threads
local variable count and array[count]: Too Many Variables to Count!
ArrayIndexOutOfBoundsException that I just can't figure out.
Java help.
Why is my type unexpected? required variable found value
Adding nested loop in order to print out a table