简体   繁体   中英

How do i write IF-ELSE Statement in Java

We are trying to write calculated field as ship date if-then-else statement into java with an eclipse (Talend DI TOOL)

Environment : we are trying to create job in talend.

source systems (sql server and excel file)---->tmap---->Target system(Excel file)

Input Expression : if-then-else formula

ship date

if ([Customer])='0000001' or 
(TRIM([Corp Acct Name]))='GENERAL' or (TRIM([Corp Acct Name]))='ABC' or (TRIM([Corp Acct Name]))='XYZ'
or (TRIM([Corp Acct Name]))='CDE' then ([ShipDate]) 
elseif DATEPART('weekday',([MABD])) =1 and [Total Lead Time]<=5 then (([MABD])-[Total Lead Time]-2)
elseif DATEPART('weekday',([MABD])) =1 and [Total Lead Time]>5 then (([MABD])-[Total Lead Time]-4)
elseif DATEPART('weekday',([MABD])) =2 and [Total Lead Time]<=1 then (([MABD])-[Total Lead Time])
elseif DATEPART('weekday',([MABD])) =2 and [Total Lead Time]<=6 then (([MABD])-[Total Lead Time]-2)
elseif DATEPART('weekday',([MABD])) =2 and [Total Lead Time]>6 then (([MABD])-[Total Lead Time]-4)
elseif DATEPART('weekday',([MABD])) =3 and [Total Lead Time]<=2 then (([MABD])-[Total Lead Time])
elseif DATEPART('weekday',([MABD])) =3 and [Total Lead Time]<=7 then (([MABD])-[Total Lead Time]-2)
elseif DATEPART('weekday',([MABD])) =3 and [Total Lead Time]>7 then (([MABD])-[Total Lead Time]-4)
elseif DATEPART('weekday',([MABD])) =4 and [Total Lead Time]<=3 then (([MABD])-[Total Lead Time])
elseif DATEPART('weekday',([MABD])) =4 and [Total Lead Time]<=8 then (([MABD])-[Total Lead Time]-2)
elseif DATEPART('weekday',([MABD])) =4 and [Total Lead Time]>8 then (([MABD])-[Total Lead Time]-4)
elseif DATEPART('weekday',([MABD])) =5 and [Total Lead Time]<=4 then (([MABD])-[Total Lead Time])
elseif DATEPART('weekday',([MABD])) =5 and [Total Lead Time]<=9 then (([MABD])-[Total Lead Time]-2)
elseif DATEPART('weekday',([MABD])) =5 and [Total Lead Time]>9 then (([MABD])-[Total Lead Time]-4)
elseif DATEPART('weekday',([MABD])) =6 and [Total Lead Time]<=4 then (([MABD])-[Total Lead Time]-1)
elseif DATEPART('weekday',([MABD])) =6 and [Total Lead Time]<=9 then (([MABD])-[Total Lead Time]-3)
elseif DATEPART('weekday',([MABD])) =6 and [Total Lead Time]>9 then (([MABD])-[Total Lead Time]-5)
elseif DATEPART('weekday',([MABD])) =7 and [Total Lead Time]<=4 then (([MABD])-[Total Lead Time]-2)
elseif DATEPART('weekday',([MABD])) =7 and [Total Lead Time]<=9 then (([MABD])-[Total Lead Time]-4)
elseif DATEPART('weekday',([MABD])) =7 and [Total Lead Time]>9 then (([MABD])-[Total Lead Time]-6)
END

We tried :Ternery operator in talend

row1.customer='0000001'|| trim(row1.CorpAcctName)='GENERAL'|| trim(row1.CorpAcctName)='ABC' or trim(row1.CorpAcctName)='XYZ or trim(row1.CorpAcctName)=='SCDE' ||?row1.ShipDate:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD)=1)-1) and row2.L+1<5?row1.MABD-row2.L+1-2:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=1)-1) and row2.L+1>5?row1.MABD-row2.L+1-4:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=2)-1) and row2.L+1<=1?row1.MABD-row2.L+1:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=2)-1) and row2.L+1<=6?row1.MABD-row2.L+1-2:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=2)-1) and row2.L+1>6?row1.MABD-row2.L+1-4:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=3)-1) and row2.L+1<=2?row1.MABD-row2.L+1:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=3)-1) and row2.L+1<=7?row1.MABD-row2.L+1-2:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=3)-1) and row2.L+1>7?row1.MABD-row2.L+1-4:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=4)-1) and row2.L+1<=3?row1.MABD-row2.L+1:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=4)-1) and row2.L+1<=8?row1.MABD-row2.L+1-2:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=4)-1) and row2.L+1>8?row1.MABD-row2.L+1-4:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=5)-1) and row2.L+1<=4?row1.MABD-row2.L+1:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=5)-1) and row2.L+1<=9?row1.MABD-row2.L+1-2:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=5)-1) and row2.L+1>9?row1.MABD-row2.L+1-4:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=6)-1) and row2.L+1<=4?row1.MABD-row2.L+1-1:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=6)-1) and row2.L+1<=9?row1.MABD-row2.L+1-3:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=6)-1) and row2.L+1>9?row1.MABD-row2.L+1-5:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=7)-1) and row2.L+1<=4?row1.MABD-row2.L+1-2:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=7)-1) and row2.L+1<=9?row1.MABD-row2.L+1-4:
Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=7)-1) and row2.L+1>9?row1.MABD-row2.L+1-6:""

Any help on it would be much appreciated ?

first you have multiple syntax errors in your expression : if you want to compare 2 Int, the correct operator is "==". (And If you want to compare two String, you should use "0000001".equals(row1.customer) ).

This expression doesn't make sense :

 Integer.toString (TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD=1)-1)

if you want to compare DAY_OF_WEEK to 1, use

(TalendDate.getPartOfDate("DAY_OF_WEEK",row1.MABD)-1) ==1

Then, using raw Ternary operator in a tMap is perhaps not the best thing to do : it is not really fit for a complex expression like this one (no comments available, no possible reusing). You should instead create a method in the "Routine" section, and give it row1.customer,row1.CorpAcctName,row1.ShipDate,row1.MABD as parameters. In this routine method , you could then use IF/ELSE operators, something that you can't use in tMap directly.

  public static int myFunction(String customer,String CorpAcctName,Integer ShipDate,Integer MABD) {
//adding comment is possible here
            if ("0000001".equals(customer).......) {

Then in tMap, you just have to call your function in the expression constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM