This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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
 
You get good luck from rubbing the belly of a tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!