Talend Connect
Virtual Summit
JOIN US!
And visit the Customer
& Community Lounge.
May 27-28, wherever you are.

[resolved] tmap convert string to char

Highlighted
Four Stars

[resolved] tmap convert string to char

Hello Talend,

I'm trying to convert a string "X" to a character 'x' in tMap
Actually my problem is that when i tried to do (row1.name == "X")?1:0 Talend never reconize "X" and always return 0 whereas there are X in my data.
Thats wierd, maybe its because my string contains only 1 character !
That's why I want to try row1.name == 'X'
My Data : 

My tMap

MS_op is string
test is integer
I also tried to trim and left(row1.name,1)

My output
 

Expected output 
X ¦ 1
X ¦ 1
¦ 0
¦ 0
what's wrong !? HALP !!

Accepted Solutions
Highlighted

Re: [resolved] tmap convert string to char

Hi,
looks like you're getting tripped up on a bit of subtle java syntax.
to get it to do what you want, you'll need to use the "<String>.equals()" String method instead of "==". 
Java uses "==" to compare object references, not to check if the contents are the same. 
give this a try in your tMap:

"X".equals(row1.name) ? 1 : 0

View solution in original post


All Replies
Highlighted
Four Stars

Re: [resolved] tmap convert string to char

I found a workaround by doing 
(row1.name==null)?0:1
but i really want if row1.name = "X" then 1 else 0
Highlighted

Re: [resolved] tmap convert string to char

Hi,
looks like you're getting tripped up on a bit of subtle java syntax.
to get it to do what you want, you'll need to use the "<String>.equals()" String method instead of "==". 
Java uses "==" to compare object references, not to check if the contents are the same. 
give this a try in your tMap:

"X".equals(row1.name) ? 1 : 0

View solution in original post

Highlighted
Four Stars

Re: [resolved] tmap convert string to char

Hi,
looks like you're getting tripped up on a bit of subtle java syntax.
to get it to do what you want, you'll need to use the "<String>.equals()" String method instead of "==". 
Java uses "==" to compare object references, not to check if the contents are the same. 
give this a try in your tMap:

"X".equals(row1.name) ? 1 : 0


Thank you, it works! 
I tried that but i did row1.name.equals("X")?1:0 and i had a null error

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog