Parses the specified string and returns a Integer instance if the string can be decoded into an integer value. The string may be an optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal ("0..."), or decimal ("...") representation of an integer. @param string
(String string)
| 133 | * if {@code string} can not be parsed as an integer value. |
| 134 | */ |
| 135 | public static Integer decode(String string) throws NumberFormatException { |
| 136 | int length = string.length(), i = 0; |
| 137 | if (length == 0) { |
| 138 | throw new NumberFormatException(); |
| 139 | } |
| 140 | char firstDigit = string.charAt(i); |
| 141 | boolean negative = firstDigit == '-'; |
| 142 | if (negative) { |
| 143 | if (length == 1) { |
| 144 | throw new NumberFormatException(string); |
| 145 | } |
| 146 | firstDigit = string.charAt(++i); |
| 147 | } |
| 148 | |
| 149 | int base = 10; |
| 150 | if (firstDigit == '0') { |
| 151 | if (++i == length) { |
| 152 | return valueOf(0); |
| 153 | } |
| 154 | if ((firstDigit = string.charAt(i)) == 'x' || firstDigit == 'X') { |
| 155 | if (++i == length) { |
| 156 | throw new NumberFormatException(string); |
| 157 | } |
| 158 | base = 16; |
| 159 | } else { |
| 160 | base = 8; |
| 161 | } |
| 162 | } else if (firstDigit == '#') { |
| 163 | if (++i == length) { |
| 164 | throw new NumberFormatException(string); |
| 165 | } |
| 166 | base = 16; |
| 167 | } |
| 168 | |
| 169 | int result = parse(string, i, base, negative); |
| 170 | return valueOf(result); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public double doubleValue() { |