One Star

Conversion from string to float

Hi,
I am getting data in text format (positional format) and want to check if the string is null then assign 0.0 else divide the incoming string by 100
trying following expression but getting error "Exception in thread "main" java.lang.Error: Unresolved compilation problems:"
row2.fta_fare_amt.equals(null) ? Float.parseFloat("0.0")
: Float.parseFloat(row2.fta_fare_amt)
Unable to figure out what's the issue, need some help.
Thanks,
Milind
6 REPLIES
Community Manager

Re: Conversion from string to float

Hello Milind
row2.fta_fare_amt.equals(null) ? Float.parseFloat("0.0")
: Float.parseFloat(row2.fta_fare_amt)

Change it to
row2.fta_fare_amt==null?  Float.parseFloat("0.0")
: Float.parseFloat(row2.fta_fare_amt)

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Conversion from string to float

Hi Shong,
Here is the error I am getting after putting the code that you mentioned above
--
Exception in component tMap_9
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Float.parseFloat(Unknown Source)
--
where input string is string and output string is float , Do I need to change the datatype ?
Thanks,
Milind
Community Manager

Re: Conversion from string to float

Hello
This code should work fine
row2.fta_fare_amt==null||row2.fta_fare_amt.equals("")?  Float.parseFloat("0.0")
: Float.parseFloat(row2.fta_fare_amt)

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Conversion from string to float

Thanks Shong ... still the same error .. there is one more Int column, if following syntax correct, getting complilation error
row2.fta_chk_dgt_tkt_doc_nbr==null||row2.fta_chk_dgt_tkt_doc_nbr("")? Integer.parseInt("0")
: Integer.parseInt(row2.fta_chk_dgt_tkt_doc_nbr)
Thanks,
Milind
One Star

Re: Conversion from string to float

looks like the error is still for the float
Exception in component tMap_9
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Float.parseFloat(Unknown Source)
Thanks,
Milind
Community Manager

Re: Conversion from string to float

Hello guy
row2.fta_chk_dgt_tkt_doc_nbr==null||row2.fta_chk_dgt_tkt_doc_nbr("")? Integer.parseInt("0")

fta_chk_dgt_tkt_doc_nbr("")? ?????!!! please check your code, there is an error in your code.
Best regards
shong
----------------------------------------------------------
Talend | Data Agility for Modern Business