将一个空字符串转换为整数 [英] Cast a Null String into Integer
本文介绍了将一个空字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以将null转换为整数。 null实际上是一个字符串,我传递在我的服务层,接受它作为一个整数。所以,每当我试图将一个null String转换为Integer,它会抛出一个异常。但我必须将null转换为整数。
Is there any way to cast a null to Integer. The null is actually a String, which i am passing in my service layer that accepts it as an Integer. So, whenever i try to cast a null String to Integer, it throws me an exception. But i have to cast the null into Integer.
感谢
推荐答案
>您不能将字符串转换为整数。但是,如果你试图将字符串转换为整数,如果你必须提供一个实现来处理 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;
}
这篇关于将一个空字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文