判断给定的字符串是否是整数,并且这个整数可以由一个32位无符号整型来表示,即范围在[0,2^32)
解答:
注意,java中的int型占4个字节,取值范围是-2^31到2^31-1,java中没有unsigned int型。
这里要判断整数处在范围[0,2^32),只能将字符串转成整数后存在一个long型变量中,然后判断范围。
public static boolean isInteger(String value) {
long l1=0;
try {
l1=Long.parseLong(value);
} catch (NumberFormatException e) {
return false;
}
if(l1>=0&&l1<Math.pow(2, 32))
return true;
return false;
}