java parseInt(String)字符串转整数

主要看如何处理正负号和循环处理字符串。

    public static int parseInt(String string) throws NumberFormatException {
        return parseInt(string, 10);
    }

    public static int parseInt(String string, int radix)
            throws NumberFormatException {
        if (string == null || radix < Character.MIN_RADIX
                || radix > Character.MAX_RADIX) {
            // BEGIN android-changed
            throw new NumberFormatException("unable to parse '"+string+"' as integer");
            // END android-changed
        }
        int length = string.length(), i = 0;
        if (length == 0) {
            // BEGIN android-changed
            throw new NumberFormatException("unable to parse '"+string+"' as integer");
            // END android-changed
        }
        boolean negative = string.charAt(i) == '-';
        if (negative && ++i == length) {
            // BEGIN android-changed
            throw new NumberFormatException("unable to parse '"+string+"' as integer");
            // END android-changed
        }

        return parse(string, i, radix, negative);
    }

以上是两上重载方法,真正转换方法如下:

	private static int 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) {
                // BEGIN android-changed
                throw new NumberFormatException("unable to parse '"+string+"' as integer");
                // END android-changed
            }
            if (max > result) {
                // BEGIN android-changed
                throw new NumberFormatException("unable to parse '"+string+"' as integer");
                // END android-changed
            }
            int next = result * radix - digit;
            if (next > result) {
                // BEGIN android-changed
                throw new NumberFormatException("unable to parse '"+string+"' as integer");
                // END android-changed
            }
            result = next;
        }
        if (!negative) {
            result = -result;
            if (result < 0) {
                // BEGIN android-changed
                throw new NumberFormatException("unable to parse '"+string+"' as integer");
                // END android-changed
            }
        }
        return result;
    }