• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

displaying a result in Label

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Blanka Tierrablanca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm getting an error that says "variable result might not have been initialized"
How do I fix it?
 
Dani Atrei
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
}
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic