I'm trying to figure out how to enter float values, as opposed to integers, and parse/convert them to floating point. The darned thing won't compile, and I can't come up with a remedy. I'd really appreciate your advice.
The is the compiler message. Ray.java:34: Incompatible type for declaration. Can�t convert int to java.lang.Float Float result = (Float.valueOf ( field1.getText ( ))).floatValue ( ) + import java.applet.Applet; import java.awt.event.*; import java.awt.*;
Hi Ray, not sure why you're getting *Can't convert int to java.lang.Float*. If I cut and paste your code, I get *Can�t convert float to java.lang.Float* which makes sense: field1.getText() -> gives String Float.valueOf(field1.getText()) -> gives Float (Float.valueOf ( field1.getText ( ))).floatValue ( ) -> gives float (note lowercase) Thus you are trying to assign a var of primitive type float to an Object reference of type Float, which doesn't work. Obviously you want float so you can perform the addition, so what would work is the following: //can add two floats float result = (Float.valueOf ( field1.getText ( ))).floatValue ( ) + (Float.valueOf ( field2.getText ( ))).floatValue ( ) ; //can call valueOf on float since valueOf() exists both for //primitive types and Object - look it up label1.setText ( String.valueOf ( result)) ; And there you go. You should also change the duplicate initialisation of field1 since addition results might otherwise be unexpected :-) And have a look at the static Float.parseFloat() method which can be used to achieve the same thing in a less convoluted way. Hope this helps, Marc
[This message has been edited by Marc (edited May 12, 2000).]