wood burning stoves*
The moose likes Beginning Java and the fly likes displaying a result in Label Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "displaying a result in Label" Watch "displaying a result in Label" New topic
Author

displaying a result in Label

Blanka Tierrablanca
Greenhorn

Joined: Feb 08, 2004
Posts: 6
I'm trying to display a result in a label but I keep getting an error message that setText can't be applied to int so I tried different things to change it to String but it wouldn't accept it. Plese help.
output.setText(result);
Thank you.
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
Hi, I think you can use this method that you can find on the javadocs classes under String

You could also have created an Integer type but I think this way is faster!
Hope it helps,
Daniel
[ February 20, 2004: Message edited by: Daniel Atrei ]

Si altas son las torres, el valor es alto - Alberti
Blanka Tierrablanca
Greenhorn

Joined: Feb 08, 2004
Posts: 6
Now I'm getting an error that says "variable result might not have been initialized"
How do I fix it?
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
Hmm, that s strange, make sure you have assigned a value to result before using it( if it s a local variable). If not, show the part of your code that you think causes the problem so we could have a better idea of what s going on.
Hope it will work, if not post your problems again, there are incredibly competent people on this forum! I was amazed!
Daniel
Blanka Tierrablanca
Greenhorn

Joined: Feb 08, 2004
Posts: 6
I've tried different things and I'm running out of ideas. I know it's probably something very simple that I'm overlooking. Here's the part that I think has problem in it. Whole program is kind of big too include (250 lines). This is where error comes from:

else {
// get input from TextField
int result;
int temperature = Integer.parseInt(input.getText().trim());

// get selected type of conversion and convert it
if (fahrenheit.getState() == true)
result = 5 * (temperature - 32) / 9;
if (celsius.getState() == true )
result = (9 * temperature / 5) + 32;

// format the output
output.setText(String.valueOf(result));
}
Lalit K Kumar
Ranch Hand

Joined: Jan 29, 2004
Posts: 32
hi,
In your code snapshot , you have'nt initialized your variable "result". though you have assigned the variable some value depending on your "if" condition , but it is'nt enough. since you are assigning the value if a particular condition gets fulfilled , it would be advisable if you initialize your "result" by some value (say 0).
this way your problem will be solved. here is the snapshot:-
else {
// get input from TextField
int result =0 ; //CHANGE MADE TO THIS LINE
int temperature = Integer.parseInt(input.getText().trim());
// get selected type of conversion and convert it
if (fahrenheit.getState() == true)
result = 5 * (temperature - 32) / 9;
if (celsius.getState() == true )
result = (9 * temperature / 5) + 32;
// format the output
output.setText(String.valueOf(result));
}
 
 
subject: displaying a result in Label
 
Similar Threads
How to write a file to a directory
Positioning scroll bar
WA #1.....word association
How do you make a form?
make text areas and textfields streach as window resizes