如何使用三元运算符将有时可以为 null 的字符串转换为 Java 中的整数?
How to use a ternary operator to convert a String that can sometimes be null into an integer in Java?
我正在使用 Talend 从 excel 文件中过滤掉一些行,它们不允许块语句。一切都必须是简单的逻辑或使用三元运算符。所以问题是我需要的 code/logic 将用于列中的每个单元格,但有些单元格是 null
,有些是 Strings
,其余的是 Strings
表示 integers
.
我的逻辑应该是这样的:
Return true if and only if PlanName == null || PlanName == 0
但如您所知,当它尝试在包含 null
的单元格或包含非数字字符串的单元格上 运行 时,它将失败。
如果没有 try-catch 或 block 语句,是否可以在 java 中包含此逻辑?这就是我现在拥有的:
input_row.PlanName == null || Integer.parseInt(input_row.PlanName) == 0
谢谢!
编辑:基本上,我只需要编写执行此操作的逻辑:
Return 如果 input_row.PlanName == null
或如果 input_row.PlanName == 0 则为真
这需要在不使用块语句或 try-catches 的情况下完成,因为我使用的是 Talend。所以我只能使用像 &&
和 ||
这样的逻辑运算符,我也可以使用三元运算符。
在你的情况下,我会选择例程:可重复使用的一堆代码,对于这种没有 if/else 等就很难实现的规则很方便
您可以在 Talend 中创建两个例程,使用可以在 tMap 或 tJavaRow 中使用的静态方法。
了解您的计划是否为数字的第一个例程:
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
然后是另一个例程:
public static boolean correctPlanName(String planName) {
if(Relational.ISNULL(planName)){
return false;
}
else{
if(!isNumeric(planName)){
return false;
}
else {
return true;
}
}
}
然后你在tMap/tJavaRow中调用Routines.correctPlanName(input_row.planName)。
它应该可以解决问题。
您可以使用 正则表达式 检查字符串是否仅包含数字,然后检查是否 num == 0
.
input_row.PlanName == null || (input_row.PlanName != null && input_row.PlanName.matches("\d+") && Integer.parseInt(input_row.PlanName) == 0)
编辑:可能矫枉过正,但要涵盖其他情况,例如浮点类型,前缀为 +
/-
的数字,你也可以这样做:
input_row.PlanName != null && input_row.PlanName.matches("[-+]?\d*\.?\d+") && Double.parseDouble(input_row.PlanName) == 0)
我正在使用 Talend 从 excel 文件中过滤掉一些行,它们不允许块语句。一切都必须是简单的逻辑或使用三元运算符。所以问题是我需要的 code/logic 将用于列中的每个单元格,但有些单元格是 null
,有些是 Strings
,其余的是 Strings
表示 integers
.
我的逻辑应该是这样的:
Return true if and only if PlanName == null || PlanName == 0
但如您所知,当它尝试在包含 null
的单元格或包含非数字字符串的单元格上 运行 时,它将失败。
如果没有 try-catch 或 block 语句,是否可以在 java 中包含此逻辑?这就是我现在拥有的:
input_row.PlanName == null || Integer.parseInt(input_row.PlanName) == 0
谢谢!
编辑:基本上,我只需要编写执行此操作的逻辑:
Return 如果 input_row.PlanName == null
或如果 input_row.PlanName == 0 则为真
这需要在不使用块语句或 try-catches 的情况下完成,因为我使用的是 Talend。所以我只能使用像 &&
和 ||
这样的逻辑运算符,我也可以使用三元运算符。
在你的情况下,我会选择例程:可重复使用的一堆代码,对于这种没有 if/else 等就很难实现的规则很方便
您可以在 Talend 中创建两个例程,使用可以在 tMap 或 tJavaRow 中使用的静态方法。
了解您的计划是否为数字的第一个例程:
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
然后是另一个例程:
public static boolean correctPlanName(String planName) {
if(Relational.ISNULL(planName)){
return false;
}
else{
if(!isNumeric(planName)){
return false;
}
else {
return true;
}
}
} 然后你在tMap/tJavaRow中调用Routines.correctPlanName(input_row.planName)。 它应该可以解决问题。
您可以使用 正则表达式 检查字符串是否仅包含数字,然后检查是否 num == 0
.
input_row.PlanName == null || (input_row.PlanName != null && input_row.PlanName.matches("\d+") && Integer.parseInt(input_row.PlanName) == 0)
编辑:可能矫枉过正,但要涵盖其他情况,例如浮点类型,前缀为 +
/-
的数字,你也可以这样做:
input_row.PlanName != null && input_row.PlanName.matches("[-+]?\d*\.?\d+") && Double.parseDouble(input_row.PlanName) == 0)