File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to write toString method to display name of Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to write toString method to display name of Object" Watch "How to write toString method to display name of Object" New topic
Author

How to write toString method to display name of Object

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

but i am getting compiler error at these lines..........


SCJP6.0,My blog Ranchers from Delhi
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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

Joined: Feb 18, 2010
Posts: 434
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

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

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

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write toString method to display name of Object
 
Similar Threads
HashMap Duplicate Element Problem
Hashmap Confusion
Doubt regarding working of equals() and hashCode() methods
Map interface with hashCode() method
HashMap and hashCode()