• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing values of an array list

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I've been working on this code and think I have it working. The only issue is that I'm not getting the values of the array list. Instead I just get some weird text and numbers when I go to print the array list. The following is my code:

Contacts Class

Personal Contacts Subclass

Business Contacts Subclass


And here is what prints out:


Can anyone point me in the right direction? I know I'm close, but I'm just not thinking about something and it's frustrating. Appreciate the assistance.
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see in the line number 36 you are printing the object type of Contacts

When you call System.out.println() method on some object; It calls the default toString() method of that class (in your case this is Contacts Class)

The default toString() methods returns the String like getClass().getName() + '@' + Integer.toHexString(hashCode()). This is happening in your case.

If you want to have a good human readable output just override the toString() method in your class Contacts. Its also a good practice to override this method.



 
Jayden Brymatt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manoj Kumar Jain. Would I have to override each of the get() methods, or just the displayContacts? I'm sorry for the greenhorn question, I'm just learning java and trying to understand when to do what, and what goes where.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you only have to override the method "public String toString()". And don't have it write the description of the Contact to the console, it should generate a String value and return it as the result of calling that method. Like Manoj said, every class has a toString() method which is inherited from Object, and overriding that to produce a useful description of an object is frequently a good thing to do.

Then change the displayContacts() method in the Contact class to simply write the result of toString() to the console. (By the way why is a class which is meant to represent a contact called "Contacts" and not "Contact"?)
 
Jayden Brymatt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification Paul Clapham. To answer your question, I was on my third or fourth iteration of scrapping and rewriting the program. I didn't want to delete my previous codes in case something was helpful, so I started renaming them, albeit subtle renaming, so I would look at the classes more carefully and know if I was working on the right one.
 
Jayden Brymatt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a new question, should I repost the code so the updates are made? Or just mark this one as resolved and post a new topic?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic