问题描述
把字符串转化成整数
在动手实现之前我们需要了解一些知识
Integer类中整数最小值和最大值的定义
//最小值 -2^31(-2147483648)
public static final int MIN_VALUE = 0x80000000;
//最大值 2^31-1(+2147483647)
public static final int MAX_VALUE = 0x7fffffff;
我们可以看到MIN_VALUE的绝对值大于MAX_VALUE,这点需要注意
加号和减号的ASCII值
加号'+' ASCII值是43,减号'-' ASCII值是45
数字 0-9的ASCII取值范围
数字 0-9的ASCII取值范围是48-57
完整实现
/**
* 把字符串转化成整数
* 参考博客地址:https://blog.csdn.net/derrantcm/article/details/46811799
* <p>
* Int值的范围-2^31(-2147483648) 到 2^31-1(+2147483647)
* <p>
* Integer类中整数最小值和最大值的定义
* public static final int MIN_VALUE = 0x80000000;
* public static final int MAX_VALUE = 0x7fffffff;
* <p>
* 加号'+' ASCII值是43,减号'-' ASCII值是45
* <p>
* 数字 0-9的ASCII取值范围是48-57
* <p>
* ASCII对照表 http://tool.oschina.net/commons?type=4
*/
public class StringToInt {
public static void main(String[] args) {
System.out.println(stringToInt("123"));
//System.out.println(stringToInt("+123"));
//System.out.println(stringToInt("-123"));
//System.out.println(stringToInt("1a123"));
//System.out.println(stringToInt("+2147483647"));
//System.out.println(stringToInt("-2147483647"));
//System.out.println(stringToInt("-2147483648"));
//System.out.println(stringToInt("+2147483648"));//超过最大的整数
}
public static int stringToInt(String numberString) {
if (numberString == null || numberString.isEmpty()) {
throw new NumberFormatException(numberString);
}
char first = numberString.charAt(0);
if (first == '-') {
return parseString(numberString, 1, false);
} else if (first == '+') {
return parseString(numberString, 1, true);
} else if (first >= '0' && first <= '9') {
return parseString(numberString, 0, true);
} else {
throw new NumberFormatException(numberString);
}
}
/**
* @param numberString 字符串
* @param index 从字符串的下标index处开始处理字符
* @param positive 是否是正数
* @return
*/
private static int parseString(String numberString, int index, boolean positive) {
if (index >= numberString.length()) {
throw new NumberFormatException(numberString);
}
int result;
//注释1处,用long类型来储存结算结果
long temp = 0;
//注释2处
long INTEGER_MAX_VALUE_ABS = 0x8000_0000L;
while (index < numberString.length()) {
if (isDigit(numberString.charAt(index))) {
temp = temp * 10 + numberString.charAt(index) - '0';
if (temp > INTEGER_MAX_VALUE_ABS) {
throw new NumberFormatException(numberString);
}
index++;
} else {
throw new NumberFormatException(numberString);
}
}
if (positive) {
//注释3处
if (temp == INTEGER_MAX_VALUE_ABS) {
throw new NumberFormatException(numberString);
} else {
result = (int) temp;
}
} else {
result = (int) -temp;
}
return result;
}
/**
* 判断是否是数字
*
* @param c 字符
* @return true 是数字,false 不是数字
*/
private static boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
}
重点分析
//注释1处,用long类型来储存结算结果
long temp = 0;
//注释2处
long INTEGER_MAX_VALUE_ABS = 0x8000_0000L;
注释1处,我们需要用long类型temp
来存储中间计算结果,因为我们的计算结果是以正数来表示的,而Inger类的MIN_VALUE的绝对值大于MAX_VALUE,也就是说我们temp
的最大值会达到2147483648
,而Integer能表示的最大值是 2147483647
,所以我们要使用long类型temp
来存储中间计算结果。
注释2处,Integer类所表示数的最大绝对值(2147483648),也要用long来表示。
if (positive) {
//注释3处
if (temp == INTEGER_MAX_VALUE_ABS) {
throw new NumberFormatException(numberString);
} else {
result = (int) temp;
}
}
注释3处,如果是正数的话,最大值是2147483647
,必须要小于2147483648
。
参考链接