Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Print Array

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 75692
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or give each of your items a proper toString() method.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Handy class, that java.util.Arrays. And if you are using the current version of
Java, you can even shorten that by one step:
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't touch me. And dont' touch this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic