• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Samantha Holmes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Samantha Holmes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • 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.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!