Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Float.

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?
Use parseFloat, like this:
Code
float float1 = Float.parseFloat(string1);
Just to add to the above. You will most likely want to put your parse float into a try catch.
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"
double value = Double.parseDouble(text);


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by N00b_n00bJun 7, 2020 11:47 PM 5 229 Jun 7, 2020 11:47 PM
Last Post: Sajuuk
[Python] by Mr.7Feb 16, 2020 03:36 AM 10 269 Feb 16, 2020 03:36 AM
Last Post: lockhead
[C] by MordiciaNov 15, 2019 07:21 PM 3 207 Nov 15, 2019 07:21 PM
Last Post: Mordicia