| 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: 1046
|
|
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
If you find any post useful, click the "plus one" sign on the right
|
 |
 |
|
|
subject: How to write toString method to display name of Object
|
|
|