如何在Java中编写IF-ELSE语句?

我们正在尝试将计算字段作为出货dateif-then-else语句写入带有eclipse的java(Talend DI TOOL)

环境:我们正在努力创造工作。

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

inputexpression式:if-then-else公式

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

我们尝试了:Ternery操作符在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:"" 

任何帮助,将不胜感激?

首先,在expression式中有多个语法错误:如果要比较2个Int,则正确的运算符是“==”。 (如果你想比较两个string,你应该使用"0000001".equals(row1.customer) )。

这个expression没有意义:

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

如果您想将DAY_OF_WEEK与1进行比较,请使用

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

然后,在tMap中使用原始的三元运算符可能不是最好的事情:它不适合这样的复杂expression式(没有可用的注释,不可能重用)。 您应该改为在“例程”部分中创build一个方法,并将row1.customer,row1.CorpAcctName,row1.ShipDate,row1.MABD作为参数。 在这个例程方法中,可以使用IF / ELSE运算符,这些运算符不能直接在tMap中使用。

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

然后在tMap中,你只需要在expression式构造函数中调用你的函数。