Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
talendtester
Creator III

[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)
Labels (2)
1 Solution

Accepted Solutions
_AnonymousUser
Specialist III

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

View solution in original post

5 Replies
Anonymous
Not applicable

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
talendtester
Creator III
Author

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)
_AnonymousUser
Specialist III

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
talendtester
Creator III
Author

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

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