有没有办法将null转换为Integer.null实际上是一个String,我在我的服务层传递,它接受它作为一个整数.因此,每当我尝试将null String转换为Integer时,它都会抛出异常.但我必须将null转换为Integer.
谢谢
Juv*_*nis 23
您无法从String转换为Integer.但是,如果您尝试将字符串转换为整数,并且您必须提供处理null字符串的实现,请查看此代码段:
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;
}
Run Code Online (Sandbox Code Playgroud)
String s= "";
int i=0;
i=Integer.parseInt(s+0);
System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
试试这个
| 归档时间: |
|
| 查看次数: |
75743 次 |
| 最近记录: |