且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将一个空字符串转换为整数

更新时间:2023-01-08 16:15:46

>您不能将字符串转换为整数。但是,如果你试图将字符串转换为整数,如果你必须提供一个实现来处理 null Strings,看看这段代码:

You cannot cast from String to Integer. However, if you are trying to convert string into integer and if you have to provide an implementation for handling null Strings, take a look at this code snippet:

String str = "...";
// suppose str becomes null after some operation(s).
int number = 0;
try
{
    if(str != null)
      number = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
    number = 0;
}