• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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.
 
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: 5307
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.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!