This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting and showing a wrapped value.

 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Doh! I mean...

return Integer.toString(number);

(It's clearly not my day.) :roll:
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic