Nine Stars

[resolved] Tmap - String to Int - And return null if null

This will convert a string to an integer:
Integer.parseInt(row1.value1)
How can I change this to work for converting a String to Int and return null there is no value?
(row1.value1==null)?null:TalendDate.parseDate("dd/MM/yyyy", row1.value1)
Is this close?
(row1.value1==null)?null:Integer.parseInt(row1.value1)
1 ACCEPTED SOLUTION

Accepted Solutions
One Star

Re: [resolved] Tmap - String to Int - And return null if null

Hi
do it like this
(row1.value1==null || row1.value1=="" )?null:Integer.parseInt(row1.value1)
or you can use

(row1.value1.length >0 )?null:Integer.parseInt(row1.value1)
Regards
Vijay.M
5 REPLIES
Community Manager

Re: [resolved] Tmap - String to Int - And return null if null

Hi
Is this close?
(row1.value1==null)?null:Integer.parseInt(row1.value1)

Yes, you are right. let us know if you don't get the expected result.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
Nine Stars

Re: [resolved] Tmap - String to Int - And return null if null

I get this error:
Exception in component tMap_1
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at talenddemosjava.myJob_0_1.myJob.tFileInputDelimited_1Process(myJob.java:1171)
at talenddemosjava.myJob_0_1.myJob.runJobInTOS(myJob.java:1682)
at talenddemosjava.myJob_0_1.myJob.main(myJob.java:1556)
One Star

Re: [resolved] Tmap - String to Int - And return null if null

Hi
do it like this
(row1.value1==null || row1.value1=="" )?null:Integer.parseInt(row1.value1)
or you can use

(row1.value1.length >0 )?null:Integer.parseInt(row1.value1)
Regards
Vijay.M
Nine Stars

Re: [resolved] Tmap - String to Int - And return null if null

Thanks mailforvijaym & shong,
I found out that my data had several bad records with less than the correct number of delimiters.
One Star

Re: [resolved] Tmap - String to Int - And return null if null

Hi Shong,
I would appreciate it if you could tell me what is the Talend best practice for the following scenario:
I receive input as an XML document and use tXMLMap to split the fields (Which are all string) but some of them are supposed to be inserted into the database as decimal and some as int. So shall I do the converting of strings to BigDecimal and int within tXMLMap and then check if the required fields have been filled in at tFilterRow or shall I do both converting and checking in tFilterRow?
Also, the filtering code is quite long in tFilterRow so shall I create routines and call them - any advice on this will be greatly appreciated.
Best regards,
Shujel