• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write toString method to display name of Object

 
Sandra Bachan
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

 
Shanky Sohar
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..........
 
Minhaj Mehmood
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!
 
Abimaran Kugathasan
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.
 
Prasad Kharkar
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic