[英]Setting null value to integer
How can i set null to integer variable initially instead of "0".我如何最初将 null 设置为整数变量而不是“0”。 I know already by default all int value is "0".
我已经知道默认情况下所有 int 值都是“0”。 I used
String.valueOf()
method etc. I dont want to print as "null".我使用了
String.valueOf()
方法等。我不想打印为“null”。 I want to print blank like " ".我想像“”一样打印空白。 I get number format exception.
我得到数字格式异常。 Please give one solution for that.
请给出一个解决方案。
Thanks in advance提前致谢
Here is the code这是代码
public class IntegerDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String id="";
System.out.println(""+Integer.parseInt(id));
}
}
I am sure you are over-complicating the problem, it is a real simple thing to do.我相信你把问题复杂化了,这是一件非常简单的事情。 Check the code below:
检查下面的代码:
Integer i = null;
System.out.println(i == null ?"":i);
You would have to stick with the java.lang.Integer
class.您必须坚持使用
java.lang.Integer
类。
String id = ""
Integer test = null;
if (!"".equals(id))
{
test = Integer.parseInt(id);
}
Creating an instance of Integer
with its primary value as null
is meaningless, but you can do this on declaration:创建一个主值为
null
的Integer
实例是没有意义的,但你可以在声明时这样做:
Integer id = null;
The reason you're getting NumberFormatException
is laid out in the documentation : 文档中列出了您收到
NumberFormatException
的原因:
An exception of type NumberFormatException is thrown if any of the following situations occurs:
如果发生以下任何一种情况,则会抛出 NumberFormatException 类型的异常:
The first argument is null or is a string of length zero.
第一个参数为 null 或长度为零的字符串。
The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
基数小于 Character.MIN_RADIX 或大于 Character.MAX_RADIX。
Any character of the string is not a digit of the specified radix, except that the first > character may be a minus sign '-' ('\-') or plus sign '+' ('\+') provided that the string is longer than length 1.
字符串的任何字符都不是指定基数的数字,除了第一个 > 字符可以是减号 '-' ('\-') 或加号 '+' ('\+') 前提是字符串比长度 1 长。
The value represented by the string is not a value of type int.
字符串表示的值不是 int 类型的值。
You can't call Integer.parseInt("")
or Integer.parseInt(null)
, with that limitation.你不能调用
Integer.parseInt("")
或Integer.parseInt(null)
,有这个限制。
If you parse null
or blank
as int it will throw numberFormatException
.如果您将
null
或blank
解析为 int 它将抛出numberFormatException
。 Check for that before parsing:在解析之前检查:
try{
System.out.println(StringUtils.isNotBlank(id)?Integer.parseInt(id):"");
}
catch(NumberFormatException e){
System.out.println("Not A Number");
}
Also be sure if string has not number by catching exception.还要通过捕获异常来确定字符串是否没有数字。
StringUtils.isNotBlank()
will check for both null
as well as blank
. StringUtils.isNotBlank()
将检查null
和blank
。
除了解析,我们可以使用Integer.valueOf(null);
Integer int = null;
int 是基元,它们的默认值与 null 不同。
The simple boxed Integer Integer i = null;
简单的盒装整数
Integer i = null;
has already been supplied.已经提供。 Here is an alternative:
这是一个替代方案:
class NullableInt {
private int value;
private boolean isNull;
public NullableInt() { isNull = true; }
public NullableInt(int value) {
this.value = value;
isNull = false;
}
public NullableInt(String intRep) {
try { value = Integer.parseInt(intRep);
} catch (NumberFormatException) {
isNull = true;
}
}
boolean isNull() { return isNull; }
void setNull(boolean toNull) { isNull = toNull; }
void setValue(int value) { this.value = value; }
int getValue() { return value; }
@Override
public String toString() { return isNull ? "" : value; }
}
NullableInt integer1 = new NullableInt(56);
NullableInt integer2 = new NullableInt();
System.out.pritnln(integer1); // >> "56"
System.out.println(integer2); // >> ""
// and finally, to address the actual question:
String id = "";
System.out.println(new NullableInt(id)); // >> ""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.