How to write if then else in tjavarow?

Highlighted
Six Stars

How to write if then else in tjavarow?

I'm trying to write a case statement in tjavarow using if then else.   This is what I've written and it's not working.  

 

output_row.Value = if (input_row.Value = "MP\/MM") {   
output_row.Value = "MP / MM";
} else if (input_row.Value = "T \u200 MM") {
output_row.Value = "T - M";
} else if (input_row.Value = "T \u200 ND") {
output_row.Value = "T - ND";
} else if (input_row.Value = "T LF") {
output_row.Value = "LF";
} else {
output_row.Value = input_row.Value; 
}


Accepted Solutions
Community Manager

Re: How to write if then else in tjavarow?

I suspect that is caused by this "MP\/MM". Change it to "MP\\/MM" and that will work. "\" is known as an escape character in Java. Certain characters have alternative meanings/uses in Strings. For example, a " character is used to enclose a String, if used inside the String without an escape character, it breaks the String. The "\" character is the escape character, so to use it literally you need to escape it.


All Replies
Fifteen Stars TRF
Fifteen Stars

Re: How to write if then else in tjavarow?

You must follow Java rules.
1st, remove "output_row.Value =" from the beginning
2nd, don't use == for strings comparison, use String.equals method. For example, you should write
if ("MP\/MM".equals(input_row.Value))

TRF
Six Stars

Re: How to write if then else in tjavarow?

After making the change per below suggestion.  Now, I'm getting following error message.

Detail Message: Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )

Community Manager

Re: How to write if then else in tjavarow?

I suspect that is caused by this "MP\/MM". Change it to "MP\\/MM" and that will work. "\" is known as an escape character in Java. Certain characters have alternative meanings/uses in Strings. For example, a " character is used to enclose a String, if used inside the String without an escape character, it breaks the String. The "\" character is the escape character, so to use it literally you need to escape it.

Six Stars

Re: How to write if then else in tjavarow?

Resolved it by adding \ front of the escape character

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

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog