Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString

 
Timo Lumme
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
im confused with toString methods
i fetch data int type and need converted on fly to string to the gui


 
W Pearce
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really all you have to do here is change the return type in the signature of getMaxID to String HOWEVER if you want to comply with javabeans standards, your method signature should really be public int getMaxID() with no arguments, you can rely on your setMaxID(int s) method to set the value. You should just call your toString() method from your GUI - that will return your LottoMaxID represented as a String object the way you have coded it here.
 
Timo Lumme
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good but i get when trying it
i get error int cannot be dereferenced

LG.button4.setText(LD.getMaxID().toString());

this so simple stuff but manage to screw anyway, arrgh ..
 
W Pearce
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try changing LG.button4.setText(LD.getMaxID().toString()); to LG.button4.setText(LD.toString());
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timo:

In java, primitives are not objects. This line is causing the problem:



Your getMaxID() method returns an int, which is a primitive. If you want to convert it to a String, try this:


Note, I'm using the Integer class, which is different from an int primitive.

John.
 
Timo Lumme
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big Thank You
(i wish i would have your java brains/knowledge.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic