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.
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
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
Si altas son las torres, el valor es alto - Alberti
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)); }
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.