< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page
rev 52979 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: TBD


1392      * Integer.parseInt} method with the indicated radix (10, 16, or
1393      * 8).  This sequence of characters must represent a positive
1394      * value or a {@link NumberFormatException} will be thrown.  The
1395      * result is negated if first character of the specified {@code
1396      * String} is the minus sign.  No whitespace characters are
1397      * permitted in the {@code String}.
1398      *
1399      * @param     nm the {@code String} to decode.
1400      * @return    an {@code Integer} object holding the {@code int}
1401      *             value represented by {@code nm}
1402      * @exception NumberFormatException  if the {@code String} does not
1403      *            contain a parsable integer.
1404      * @see java.lang.Integer#parseInt(java.lang.String, int)
1405      */
1406     public static Integer decode(String nm) throws NumberFormatException {
1407         int radix = 10;
1408         int index = 0;
1409         boolean negative = false;
1410         Integer result;
1411 
1412         if (nm.length() == 0)
1413             throw new NumberFormatException("Zero length string");
1414         char firstChar = nm.charAt(0);
1415         // Handle sign, if present
1416         if (firstChar == '-') {
1417             negative = true;
1418             index++;
1419         } else if (firstChar == '+')
1420             index++;
1421 
1422         // Handle radix specifier, if present
1423         if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
1424             index += 2;
1425             radix = 16;
1426         }
1427         else if (nm.startsWith("#", index)) {
1428             index ++;
1429             radix = 16;
1430         }
1431         else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
1432             index ++;




1392      * Integer.parseInt} method with the indicated radix (10, 16, or
1393      * 8).  This sequence of characters must represent a positive
1394      * value or a {@link NumberFormatException} will be thrown.  The
1395      * result is negated if first character of the specified {@code
1396      * String} is the minus sign.  No whitespace characters are
1397      * permitted in the {@code String}.
1398      *
1399      * @param     nm the {@code String} to decode.
1400      * @return    an {@code Integer} object holding the {@code int}
1401      *             value represented by {@code nm}
1402      * @exception NumberFormatException  if the {@code String} does not
1403      *            contain a parsable integer.
1404      * @see java.lang.Integer#parseInt(java.lang.String, int)
1405      */
1406     public static Integer decode(String nm) throws NumberFormatException {
1407         int radix = 10;
1408         int index = 0;
1409         boolean negative = false;
1410         Integer result;
1411 
1412         if (nm.isEmpty())
1413             throw new NumberFormatException("Zero length string");
1414         char firstChar = nm.charAt(0);
1415         // Handle sign, if present
1416         if (firstChar == '-') {
1417             negative = true;
1418             index++;
1419         } else if (firstChar == '+')
1420             index++;
1421 
1422         // Handle radix specifier, if present
1423         if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
1424             index += 2;
1425             radix = 16;
1426         }
1427         else if (nm.startsWith("#", index)) {
1428             index ++;
1429             radix = 16;
1430         }
1431         else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
1432             index ++;


< prev index next >