This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes setText Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "setText" Watch "setText" New topic
Author

setText

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I have a value that has to be defined as a float. I'm trying to right this value to a text field using setText but everything that I have tried gives me a compile error. How do I handle this?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24193
    
  34

setText() wants a String as an argument, so you need to convert your float to a String. There are (as is often the case) several ways to do this, but easy one is the static method String.valueOf(); there's an overloaded version of this for just about every primitive type. So you could say


[Jess in Action][AskingGoodQuestions]
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hello,
There is another way of type casting a Float value to a String value.
float x = 0.0f;
String text = Float.toString(x);
Hope it is worth to you.


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
 
Don't get me started about those stupid light bulbs.
 
subject: setText
 
It's not a secret anymore!