This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

HeadFirstJava SortMountains Output  RSS feed

 
Greenhorn
Posts: 19
Java jQuery Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a question about a lesson in HeadFirstJava book about SortMountains (chapter 16's "Sharpen your pencil").

So here's the code I've got:





The output:





While the book suggest that the output should be like this:





I have no idea where the mistake is. Any help?

Thank you.
 
Bartender
Posts: 5627
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Joshua Soeng
Greenhorn
Posts: 19
Java jQuery Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Your Mountain class needs a toString() method to format its output.



Thanks Carey, but could you please give me an example how to do that? or maybe there's a good reference?
 
Carey Brown
Bartender
Posts: 5627
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The toString() method is inherited from Object. Object's toString() method returns the hash code for your object by default, which is what you were seeing. So to format the data to something other than the default you must override Object#toString() and write one of you own inside your Mountain class.
This is an example. You may decide to format the returned String differently.
 
Joshua Soeng
Greenhorn
Posts: 19
Java jQuery Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:The toString() method is inherited from Object. Object's toString() method returns the hash code for your object by default, which is what you were seeing. So to format the data to something other than the default you must override Object#toString() and write one of you own inside your Mountain class.
This is an example. You may decide to format the returned String differently.



Thank you very much Carey, it's all clear now. I really appreciate your help and explanation.
 
Crusading Chameleon likes the size of this ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!