Search code examples
javatalend

Using Conditional Operator for If..Else


I am trying to modify an existing expression in Talend Open Studio using the IF..ELSE logic with a conditional operator.

The logic is:

    IF (row73.Month01 == NULL || row73.Month01 == 0.0) THEN 1
    ELSE
    { 
        IF (row46.month01 == NULL || row46.Month01 == 0.0) THEN 1
        ELSE (row46.Month01*100.0f/row73.Month01)
    }

The expression with the conditional operator I am entering is:

    (row73.Month01==null||row73.Month01==0.0)?1: (row46.Month01==null||row46.Month01==0.0)?1: (row46.Month01*100.0f/row73.Month01))

However, when Testing the expression in Talend, it said "Unresolved compilaton". I tried to test the above in a standalone java skeleton file, but then I am getting the "source_file.java:13: error: on "?"

What am I doing wrong in the above expression format for Talend?

Greatly appreciate any input.

TIA, Bee


Realised one more thing. The datatype for Month01 is defined as Float in the workflow. Given that, is the Null check even valid at all? Since this is a primitive data type and not an object. Shouldnt just the check for row73.Month01 == 0.0 suffice?

Currently, this job is failing on a Null Pointer exception since Month01 has null values in the input source. Hence trying to handle it.

Thanks again.


Solution

  • there is a syntax error in

    (row73.Month01==null||row73.Month01==0.0)?1: 
    (row46.Month01==null||row46.Month01==0.0)?1: 
    (row46.Month01*100.0f/row73.Month01))
    

    You need an opening parenthesis in the second line:

    (row73.Month01==null||row73.Month01==0.0)?1: 
    ((row46.Month01==null||row46.Month01==0.0)?1: 
    (row46.Month01*100.0f/row73.Month01))