• 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

How to make my code print the text insted of the hashcode?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
I'm trying to learn java by myself but I've been stuck in this code for too long.
I don't know if its just the printing part that is wrong, but if something in my other classes is also wrong.
The purpose of the code is to create a person, and print her/his data, including an ArrayList of hobbies. I'm currently struggling with the printing of the hobbies.

Here goes the code:

PERSON CLASS:


HOBBIES CLASS:


MAIN:


And the output is:


I know I've created a list in the Hobbies class that I think I'm not using at all. Its begging purpose was to also create a findHobby method, so the input would have to be on that list to be added but since I can't even print this right, I gave up on that.
I'm very new at this so please don't be mean, and I googled a lot in the past days to try and fix this but I couldn't.
~Thank you for your time
 
Samantha Holmes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, in the main class there is a "carolina" where it was supposed to be john, I was trying to protect the identity of my friend but it's only a first name, so it's okay :P
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Samantha Holmes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:



It doesn't work, I might have done something wrong somewhere it the other classes.
 
Samantha Holmes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samantha Holmes wrote:

Swastik Dey wrote:



It doesn't work, I might have done something wrong somewhere it the other classes.


And i can't figure out what it is
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should override toString() for your Person and Hobbies class. Then, when you print an object of one of these types, it will use the String returned from toString().

Your Hobbies class should not extend Person. A person has hobbies, hobbies are not a person.
 
Samantha Holmes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You should override toString() for your Person and Hobbies class. Then, when you print an object of one of these types, it will use the String returned from toString().

Your Hobbies class should not extend Person. A person has hobbies, hobbies are not a person.



Thanks for the reply mate.
How do I override it?
I know the method but what should i put inside it?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever you want to display when the object is printed. Do you know how to return a string from a method?
 
Samantha Holmes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samantha Holmes wrote:

Stephan van Hulst wrote:You should override toString() for your Person and Hobbies class. Then, when you print an object of one of these types, it will use the String returned from toString().

Your Hobbies class should not extend Person. A person has hobbies, hobbies are not a person.



Thanks for the reply mate.
How do I override it?
I know the method but what should i put inside it?




What should I put in the blank? And should I put this method in both Person and Hobbies?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samantha Holmes wrote:. . . What should I put in the blank?

What do you want to be printed? That is what goes in there. Consider using the  method which works like what is shown in this Java™ Tutorials section instead of the + operator,

And should I put this method in both Person and Hobbies?

If you want to print the details of a Person or a Hobby object, then yes, but the methods will be different. Since your hobby class has a List field, try printing the List on its own to see what the output looks like.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a nit: You have singular and plural swapped.
Your class Hobbies should be Hobby, and your ArrayList should be hobbies. A class only defines a single object whereas a collection holds many.
So you should end up with:
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is more confusion about plural and singular elsewhere. The Hobbies class encapsulates a List, so maybe it shou‍ld be plural after all, but is there a Hobby class at all?
 
reply
    Bookmark Topic Watch Topic
  • New Topic