![]() |
|
[Java] java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Float. - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Float. (/showthread.php?tid=4045394) |
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Float. - President Holy - Feb 6, 2014 How would I cast a String to a Float? In my java program, i am reading from a file (writing is really easy). I am reading stuff like: reach.distance:2.0 bot.speed:true killaura.silent:true clone.copy:true clone.mode:5.7 bot.name:Hi There Bro spammer.message:Sup my lil' awesome puppet ;) So, anyone know how to cast a string to a float? * - SIM-2015410 - Feb 6, 2014 Use parseFloat, like this: Code float float1 = Float.parseFloat(string1);* - ๖ۣۜSerendipity - Feb 7, 2014 Just to add to the above. You will most likely want to put your parse float into a try catch. * - Knautschtabak - Feb 8, 2014 Since you are already reading these as strings, I suggest you convert the string to double containing the floating point number instead of parsing as float/double right away (that approach will save you some trouble people often get while mixing string and primitive value parsing from command line). You should use double instead of float as your datatype. There are really only very few occasions where you actually need the float data type. Code String text = "5.89" |