Search code examples
javac++conditional-statementstalendtmap

How do i write Decoding function Using Conditional operator in java


We are trying to map data using Talend DI tool. In that, we have to capture transformation which is related to Conditional operator.(Because of limitation of tool it won't allow if-then-else syntax instead it does support conditional operator.

Sample Data :

I am trying to write this expression into talend tmap component . How to write this expression into tmap component expression builder using ternary operator. Additional, i have to check for null values.

case when [TCode]='(00) PRE-PAID' then '00'when[TCode]='(01) C.O.D.' then '01'when[TCode]='(02) EOM' then '02'when[TCode]='10' then '(10) NET 10 DAYS'when[TCode]='15' then '(15) NET 15 DAYS'when[TCode]='21' then '(21) 2 % 30 NET 31'when[TCode]='23' then '(23) 2% NET 30 DAYS'when[TCode]='3' then '(3) CHECK'when[TCode]='30' then '(30) NET 30 DAYS' else [TCode]end as TCode

Tried this conditional operator :

"(00) PRE-PAID".equals(row.tCode) ?"00"  :
 "(01) C.O.D".equals(row.tCode) ?"01" :
"(02) EOM".equals(row.tCode) ? "02" :
"Unknown"

Getting error when tried above conditional operator :

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    XML_API_tXMLMap_1 cannot be resolved to a type
    XML_API_tXMLMap_1 cannot be resolved to a type
    Syntax error on token ""(00) PRE-PAID"", delete this token

Thanks in advance !


Solution

  • Analysis

    According to the link in your comment, you try to execute the following:

    row1.tcode==null?null:row1.tcode.length()==0?null:row1.tcode.toUpperCase()
    "(00) PRE-PAID".equals(row.tCode) ?"00"  :
    "(01) C.O.D".equals(row.tCode) ?"01" :
    "(02) EOM".equals(row.tCode) ? "02" :
    "Unknown"
    

    and you are getting the following error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        XML_API_tXMLMap_1 cannot be resolved to a type
        XML_API_tXMLMap_1 cannot be resolved to a type
        Syntax error on token ""(00) PRE-PAID"", delete this token
    

    NOTE: Java is a case sensitive language. So either tcode or tCode is correct, not both.

    Explanation

    You provided two separate rows of code and Java doesn't know how to interpret this.

    Your first line of code is (usually ended with a ;):

    row1.tcode==null?null:row1.tcode.length()==0?null:row1.tcode.toUpperCase()
    

    and the second line (albeit it has more lines in itself it is seen as "one line") of code is:

    "(00) PRE-PAID".equals(row.tCode) ?"00"  :
    "(01) C.O.D".equals(row.tCode) ?"01" :
    "(02) EOM".equals(row.tCode) ? "02" :
    "Unknown"
    

    We need to put those two instructions together.

    Solution

    (row1.tCode != null && !row1.tCode.equals("")) ? (
    "(00) PRE-PAID".equals(row.tCode.toUpperCase()) ? "00" :
    "(01) C.O.D".equals(row.tCode.toUpperCase()) ? "01" :
    "(02) EOM".equals(row.tCode.toUpperCase()) ? "02" :
    "Unknown") : "Unknown"
    

    Alternatively, to shorten the first row, you could set a Default value for tCode if that makes any sense. The default value could be "" and you don't have to check for null anymore. Again, this depends on your use case.