[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

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

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
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 GARNER 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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Definitive Guide to Data Quality

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

Download