aspose file tools*
The moose likes Beginning Java and the fly likes getting and showing a wrapped value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting and showing a wrapped value." Watch "getting and showing a wrapped value." New topic
Author

getting and showing a wrapped value.

ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
I'm having trouble getting the 17 to display where the "_____'s" are. I cannot simply plug in 17. I have tried various things and I cannot get any to work. Any guidance/suggestions/help would be appreciated. Thanks.

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ken zemaitis:
I'm having trouble getting the 17 to display where the "_____'s" are...

Doesn't it work to just to use x?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
Originally posted by marc weber:

Doesn't it work to just to use x?


I tried to use x and my System.out.println gave me the response "Number@108786b is ODD." I didn't understand why I got that either.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by marc weber:
... Doesn't it work to just to use x?

Oh, wait...

I thought you were using java.lang.Number, but I see that you've defined your own Number class.

You should override the toString method in your Number class to provide a meaningful String...

This will be called implicitly in the println simply by referencing the object.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by marc weber:

Doh! I mean...

return Integer.toString(number);

(It's clearly not my day.) :roll:
ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
Originally posted by marc weber:
<hr></blockquote>
Doh! I mean...

return Integer.toString(number);

(It's clearly not my day.) :roll: [/QB]


Ahh! Thank you, that makes sense. I guess I have one more question, do you know if it is possible to make this program display "17 is ODD" without changing the Number class?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ken zemaitis:
...do you know if it is possible to make this program display "17 is ODD" without changing the Number class?

Yes, you can simply call the getNumber method to get an int.
[ April 07, 2006: Message edited by: marc weber ]
ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
Originally posted by marc weber:

Yes, you can simply call the getNumber method to get an int.

[ April 07, 2006: Message edited by: marc weber ]


I have a lot of reading to do. Thank you very much.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ken zemaitis:
... I tried to use x and my System.out.println gave me the response "Number@108786b is ODD." I didn't understand why I got that either.

All classes already have a toString method inherited from the base class Object. The implementation of toString in Object is to return a String concatenation of the class name, an "at" sign, and a memory location. This is why the method needs to be overridden in subclasses to return a more meaningful String.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting and showing a wrapped value.