Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString

 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to display the value of 'sum1' after doing some math with it, but i keep getting: "average: gettinganestimate.Average@5f893efe"
someone said it had to do with it being and object and to use a toString method, but that isn't working out too well; I keep getting errors within the method. I had solved the problem for like half a second before, but then i did something and it went away. Any suggestions?
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kirstie Fran wrote:
someone said it had to do with it being and object and to use a toString method, but that isn't working out too well; I keep getting errors within the method. I had solved the problem for like half a second before, but then i did something and it went away. Any suggestions?


It would help if you tell us the details of what you did -- TellTheDetails.

Henry
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






and i end up getting things like:

run:
0: 10.113957033713675 average: gettinganestimate.Average@2b86c6b2
1: 6.467450812669018 average: gettinganestimate.Average@2b86c6b2
...

Sum 2 is greater
Final averages: gettinganestimate.Average@2b86c6b2, gettinganestimate.Average@1d5ee671
BUILD SUCCESSFUL (total time: 0 seconds)
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You didn't override the toString() method. The toString() method inherited from the Object class, just states the object class type and the identity hash code.

Henry
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well one of the suggestions that netbeans gave me was to place "@override" just outside of the toString() Method, but that didn't work out to well for me. So how would i actually go about doing this? because i want the actually average to appear
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kirstie Fran wrote:So how would i actually go about doing this? because i want the actually average to appear


Well, you need to create a valid toString() method. And in that method, you need to return a String object -- which is created from the value contained in the sum1 variable.... For that, take a look at the java.lang.Double class.

Henry
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i got that part sorted out now.
thanks!
i'm just having some issues with the "average" class
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What went wrong when yo added @Override? Did you get a "doesn't override" compiler error? That is the whole idea of @Override; if you make a tiny spelling mistake you get a compiler error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic