如何在Java中使用三元运算符将可能为null的字符串转换为整数?

huangapple go评论64阅读模式
英文:

How to use a ternary operator to convert a String that can sometimes be null into an integer in Java?

问题

我正在使用Talend从Excel文件中筛选出一些行,而他们不允许使用块语句。所有逻辑都必须是简单逻辑或者使用三元运算符。所以问题是,我需要的代码/逻辑将在列中的每个单元格中使用,但是其中一些单元格为 null,一些为 字符串,其余的是代表 整数字符串

我的逻辑需要是这样的:
仅当 PlanName == null || PlanName == 0 时返回 true,但是正如你所知,当它尝试在包含 null 或包含非数字字符串的单元格上运行时,它将失败。

在没有使用 try-catch 或块语句的情况下,是否可能在Java中实现这种逻辑?这是我目前的代码:

input_row.PlanName == null || Integer.parseInt(input_row.PlanName) == 0

谢谢!

编辑:基本上,我只需要编写能够实现以下逻辑的代码:
如果 input_row.PlanName == null 或者 input_row.PlanName == 0,则返回 true
这需要在不使用块语句或 try-catch 的情况下完成,因为我正在使用Talend。所以我只能使用逻辑运算符如 &&||,还可以使用三元运算符。

英文:

I am using Talend to filter out some rows from an excel file and they don't allow block statements. Everything has to be simple logic or using the ternary operator. So the problem is that the code/logic I need will be used across every cell in the column, BUT some of the cells are null, some are Strings and the rest are Strings that represent integers.

My logic needs to be this:
Return true if and only if PlanName == null || PlanName == 0 but as you can tell, it will fail when it tries to run this on a cell that contains the null or the cell that contains a String that isn't a number.

Is it possible to have this logic in java without the try-catch or block statements? This is what I have right now:

input_row.PlanName == null || Integer.parseInt(input_row.PlanName) == 0

Thanks!

Edit: Basically, I just need to write logic that does this:
Return true if input_row.PlanName == null OR if input_row.PlanName == 0
This needs to be done without using block-statements or try-catches because I am using Talend. So I can only use logical operators like && and || and I can use ternary operators as well.

答案1

得分: 3

在你的情况下,我会选择使用"例程":可重用的代码集合,在处理那种需要使用 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) 即可。这应该能解决问题。

英文:

In your situation, i'll go for routines : reusable bunch of code, handy for this kind of rules that would be hard to implement without if/else etc.

You can create two Routines in Talend, with static methods that you would be able to use in a tMap or a tJavaRow.

First Routine to know if your plan is a numeric or not :

  public static boolean isNumeric(String strNum) {
        if (strNum == null) {
            return false;
        }
        try {
            double d = Double.parseDouble(strNum);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

Then another routine like :

public static boolean correctPlanName(String planName) {
   if(Relational.ISNULL(planName)){
   return false;
   }
   else{
       if(!isNumeric(planName)){
           return false;
       }
       else {
          return true;
       }
   }

}
Then you call Routines.correctPlanName(input_row.planName) in tMap/tJavaRow.
It should do the trick.

答案2

得分: 1

你可以使用 正则表达式 来检查字符串是否仅包含数字,然后检查 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)
英文:

You can use a regular expression to check if the String only contains digits, then check if num == 0.

input_row.PlanName == null || (input_row.PlanName != null && input_row.PlanName.matches("\\d+") && Integer.parseInt(input_row.PlanName) == 0) 

Edit: Probably overkill but to cover other cases e.g. floating point types, numbers prefixed with +/-, you could also do:

input_row.PlanName != null && input_row.PlanName.matches("[-+]?\\d*\\.?\\d+") && Double.parseDouble(input_row.PlanName) == 0) 

huangapple
  • 本文由 发表于 2020年10月1日 21:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64156256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定