java 字串轉化為整數溢位問題處理
阿新 • • 發佈:2019-01-09
1、思路及注意事項
概括起來有幾種情況
1)字串開頭是“+”號或“-”號的處理
2)非法字元的判斷(不是數字)
3)整數溢位問題。
看看Java函式庫中的Integer.parseInt(String sting)的原始碼如何處理這些問題的。
- /**
- * Parses the specified string as a signed decimal integer value. The ASCII
- * character \u002d ('-') is recognized as the minus sign.
- *
- * @param string
- * the string representation of an integer value.
- * @return the primitive integer value represented by {@code string}.
-
* @throws NumberFormatException
- * if {@code string} cannot be parsed as an integer value.
- */
- publicstaticint parseInt(String string) throws NumberFormatException {
- return parseInt(string, 10);
- }
- /**
- * Parses the specified string as a signed integer value using the specified
-
* radix. The ASCII character \u002d ('-') is recognized as the minus sign.
- *
- * @param string
- * the string representation of an integer value.
- * @param radix
- * the radix to use when parsing.
- * @return the primitive integer value represented by {@code string} using
- * {@code radix}.
- * @throws NumberFormatException
-
* if {@code string} cannot be parsed as an integer value,
- * or {@code radix < Character.MIN_RADIX ||
- * radix > Character.MAX_RADIX}.
- */
- publicstaticint parseInt(String string, int radix) throws NumberFormatException {
- if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
- thrownew NumberFormatException("Invalid radix: " + radix);
- }
- if (string == null) {
- throw invalidInt(string);
- }
- int length = string.length(), i = 0;
- if (length == 0) {
- throw invalidInt(string);
- }
- boolean negative = string.charAt(i) == '-';
- if (negative && ++i == length) {
- throw invalidInt(string);
- }
- return parse(string, i, radix, negative);
- }
- privatestaticint parse(String string, int offset, int radix, boolean negative) throws NumberFormatException {
- int max = Integer.MIN_VALUE / radix;
- int result = 0, length = string.length();
- while (offset < length) {
- int digit = Character.digit(string.charAt(offset++), radix);
- if (digit == -1) {
- throw invalidInt(string);
- }
- if (max > result) {
- throw invalidInt(string);
- }
- int next = result * radix - digit;
- if (next > result) {
- throw invalidInt(string);
- }
- result = next;
- }
- if (!negative) {
- result = -result;
- if (result < 0) {
- throw invalidInt(string);
- }
- }
- return result;
- }
- publicclass StringToIntTest {
- /**
- * @author 曹豔豐 北京大學
- */
- publicstaticvoid main(String[] args) {
- // TODO 自動生成的方法存根
- try {
- System.out.println(parseInt("cao21'''474fefda8364fe7"));
- System.out.println(parseInt("-2147483648"));
- System.out.println(parseInt("-2147483651"));
- System.out.println(parseInt("-2147483648"));
- System.out.println(parseInt("-21474836410"));
- } catch (MyException e) {
- // TODO 自動生成的 catch 塊
- e.printStackTrace();
- }
- }
- privatestaticint parseInt(String string) throws MyException {
- /* 異常情況1:字串為null */
- if (string == null) {
- thrownew MyException("字串為null!");
- }
- int length = string.length(), offset = 0;
- /* 異常情況2:字串長度為0 */
- if (length == 0) {
- thrownew MyException("字串長度為0!");
- }
- boolean negative = string.charAt(offset) == '-';
- /* 異常情況3:字串為'-' */
- if (negative && ++offset == length) {
- thrownew MyException("字串為:'-'!");
- }
- int result = 0;
- char[] temp = string.toCharArray();
- while (offset < length) {
- char digit = temp[offset++];
- if (digit <= '9' && digit >= '0') {
- int currentDigit = digit - '0';
- /*
- * 異常情況4:已經等於Integer.MAX_VALUE / 10,判斷要新增的最後一位的情況:
- * 如果是負數的話,最後一位最大是8 如果是正數的話最後一位最大是7
- */
- if (result == Integer.MAX_VALUE / 10) {
- if ((negative == false && currentDigit > 7)
- || (negative && currentDigit > 8)) {
- thrownew MyException("溢位!");
- }
- /*
- * 異常情況5:已經大於Integer.MAX_VALUE / 10