[resolved] tmap convert string to char

Highlighted
One Star

[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

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

All Replies
One Star

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

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
One Star

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

Cloud Free Trial

Try Talend Cloud free for 30 days.

Tutorial

Introduction to Talend Open Studio for Data Integration.

Definitive Guide to Data Integration

Practical steps to developing your data integration strategy.

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.