• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Output Troubles  RSS feed

 
Ryan Mills
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I'm pretty new to programming still, and I'm struggling with this one for some reason. Output is either null or Flower@(random letters and numbers).



addFlower:


displayFlower:


Flower.java:
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
That's how Java outputs objects by default. To make it look nicer/more useful, you add a method to Flower like:

public String toString() {
return name;
}
 
Ryan Mills
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following method in there:



Would that be separate than that?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. toString() is a special method that Java calls when you try to print an object. If you don't implement your own, it uses the one Java has provided (by inheritance) which produces the "random number" output you mentioned.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!