File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes toString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString" Watch "toString" New topic
Author

toString

Kirstie Fran
Ranch Hand

Joined: Feb 16, 2011
Posts: 33

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
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kirstie Fran
Ranch Hand

Joined: Feb 16, 2011
Posts: 33







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
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40


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

Joined: Feb 16, 2011
Posts: 33

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
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

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

Joined: Feb 16, 2011
Posts: 33

Ok, i got that part sorted out now.
thanks!
i'm just having some issues with the "average" class
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString