public static int parseInt ( char [ ] s , int beginIndex , int endIndex ) throws NumberFormatException { s = Objects . requireNonNull ( s ) ; if ( beginIndex < 0 || beginIndex > endIndex || endIndex > s . length ) { throw new IndexOutOfBoundsException ( ) ; } 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" ) ; } boolean negative = false ; int i = beginIndex ; int limit = - Integer . MAX_VALUE ; if ( i < endIndex ) { char firstChar = s [ i ] ; if ( firstChar < ' ' ) { // Possible leading "+" or "-" if ( firstChar == ' ' ) { negative = true ; limit = Integer . MIN_VALUE ; } else if ( firstChar != ' ' ) { throw numberFormatExceptionforCharSequence ( s , beginIndex , endIndex , i ) ; } i ++ ; if ( i == endIndex ) { // Cannot have lone "+" or "-" throw numberFormatExceptionforCharSequence ( s , beginIndex , endIndex , i ) ; } } int multmin = limit / radix ; int result = 0 ; while ( i < endIndex ) { // Accumulating negatively avoids surprises near MAX_VALUE int digit = digit ( s [ i ] ) ; if ( digit < 0 || result < multmin ) { throw numberFormatExceptionforCharSequence ( s , beginIndex , endIndex , i ) ; } result *= radix ; if ( result < limit + digit ) { throw numberFormatExceptionforCharSequence ( s , beginIndex , endIndex , i ) ; } i ++ ; result -= digit ; } return negative ? result : - result ; } else { return 0 ; } }
copy of Integer . parseInt