简体   繁体   English

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

[英]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)我们正在尝试使用 Eclipse (Talend DI TOOL) 将计算字段作为发货日期 if-then-else 语句写入 java

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输入表达式:if-then-else 公式

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我们尝试过:talend 中的 Ternery 运算符

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 "==".首先,您的表达式中有多个语法错误:如果您想比较 2 个 Int,则正确的运算符是“==”。 (And If you want to compare two String, you should use "0000001".equals(row1.customer) ). (如果你想比较两个字符串,你应该使用"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如果要将 DAY_OF_WEEK 与 1 进行比较,请使用

(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).然后,在 tMap 中使用原始三元运算符可能不是最好的做法:它并不真正适合像这样的复杂表达式(没有可用的注释,不可能重用)。 You should instead create a method in the "Routine" section, and give it row1.customer,row1.CorpAcctName,row1.ShipDate,row1.MABD as parameters.您应该改为在“例程”部分中创建一个方法,并将其指定为 row1.customer,row1.CorpAcctName,row1.ShipDate,row1.MABD 作为参数。 In this routine method , you could then use IF/ELSE operators, something that you can't use in tMap directly.在这个常规方法中,您可以使用 IF/ELSE 运算符,这是您不能直接在 tMap 中使用的东西。

  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.然后在 tMap 中,您只需在表达式构造函数中调用您的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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