| 发表于:2007-08-28 11:04:121楼 得分:0 |
/** * parses the string argument as a signed integer in the radix * specified by the second argument. the characters in the string * must all be digits of the specified radix (as determined by * whether {@link java.lang.character#digit(char, int)} returns a * nonnegative value), except that the first character may be an * ascii minus sign <code> '- ' </code> ( <code> '\u002d ' </code> ) to * indicate a negative value. the resulting integer value is returned. * <p> * an exception of type <code> numberformatexception </code> is * thrown if any of the following situations occurs: * <ul> * <li> the first argument is <code> null </code> or is a string of * length zero. * <li> the radix is either smaller than * {@link java.lang.character#min_radix} or * larger than {@link java.lang.character#max_radix}. * <li> any character of the string is not a digit of the specified * radix, except that the first character may be a minus sign * <code> '- ' </code> ( <code> '\u002d ' </code> ) provided that the * string is longer than length 1. * <li> the value represented by the string is not a value of type * <code> int </code> . * </ul> <p> * examples: * <blockquote> <pre> * parseint( "0 ", 10) returns 0 * parseint( "473 ", 10) returns 473 * parseint( "-0 ", 10) returns 0 * parseint( "-ff ", 16) returns -255 * parseint( "1100110 ", 2) returns 102 * parseint( "2147483647 ", 10) returns 2147483647 * parseint( "-2147483648 ", 10) returns -2147483648 * parseint( "2147483648 ", 10) throws a numberformatexception * parseint( "99 ", 8) throws a numberformatexception * parseint( "kona ", 10) throws a numberformatexception * parseint( "kona ", 27) returns 411787 * </pre> </blockquote> * * @param s the <code> string </code> containing the integer * representation to be parsed * @param radix the radix to be used while parsing <code> s </code> . * @return the integer represented by the string argument in the * specified radix. * @exception numberformatexception if the <code> string </code> * does not contain a parsable <code> int </code> . */ public static int parseint(string s, int radix) throws numberformatexception { if (s == null) { throw new numberformatexception( "null "); } if (radix < character.min_radix) { throw new numberformatexception( "radix " + radix + " less than character.min_radix "); } if (radix > character.max_radix) { throw new numberformatexception( "radix " + radix + " greater than character.max_radix "); } int result = 0; boolean negative = false; int i = 0, max = s.length(); int limit; int multmin; int digit; if (max > 0) { if (s.charat(0) == '- ') { negative = true; limit = integer.min_value; i++; } else { limit = -integer.max_value; } multmin = limit / radix; if (i < max) { digit = character.digit(s.charat(i++),radix); if (digit < 0) { throw numberformatexception.forinputstring(s); } else { result = -digit; } } while (i < max) { // accumulating negatively avoids surprises near max_value digit = character.digit(s.charat(i++),radix); if (digit < 0) { throw numberformatexception.forinputstring(s); } if (result < multmin) { throw numberformatexception.forinputstring(s); } result *= radix; if (result < limit + digit) { throw numberformatexception.forinputstring(s); } result -= digit; } } else { throw numberformatexception.forinputstring(s); } if (negative) { if (i > 1) { return result; } else { /* only got "- " */ throw numberformatexception.forinputstring(s); } } else { return -result; } } | | |
|