Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write toString method to display name of Object  RSS feed

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below code is a hybrid of Sierra/Bates Chapter 7, Question 4, and code from http://www.leepoint.net/notes-java/data/collections/maps/map_iteration.html




Output:

3
ToDos@12b6651 = doLaundry
ToDos@1888759 = cleanAttic
ToDos@4a5ab2 = payBills



From what I understand, if we implement hashCode, only two entries are in HashMap because Monday-payBills overwrites Monday-doLaundy, whereas if we leave it alone both doLaundry and payBills are present.

Question: How to write toString function so that it gives neat output, i.e.

3
t1 = doLaundry
t2 = cleanAttic
t3 = payBills

 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i am getting compiler error at these lines..........
 
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sandra Bachan wrote:From what I understand, if we implement hashCode, only two entries are in HashMap because Monday-payBills overwrites Monday-doLaundy, whereas if we leave it alone both doLaundry and payBills are present.



Well it's because your implemented hashCode method is not well formed.

Sandra Bachan wrote:Question: How to write toString function so that it gives neat output, i.e.

3
t1 = doLaundry
t2 = cleanAttic
t3 = payBills



What I understand, you want to output like

for above out put just edit your ToDos class as per following listed code:




HTH

Minhaj
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

phil sohar wrote:but i am getting compiler error at these lines..........




How are you compiling, i.e. if using command-line, what are you typing.

And what exactly is the error?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Minhaj Mehmood wrote:
What I understand, you want to output like

for above out put just edit your ToDos class as per following listed code:




HTH

Minhaj




Thanks!
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What toString() method does, describes the object meaningfully. If it's your job to describe your own object. So it can be a state, or some other, but it should differentiate the two object, otherwise, no meaning.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and also
toString() method on the object gets called when we try to print the object using println() printf() or the print() method
so we do not need to call that method explicitly
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!