Skip to main content

NumberFormatException

java.lang.NumberFormatException

NumberFormatException is described in the javadoc comments as:

Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
author: unascribed version: 1.20, 12/19/03 see: java.lang.Integer#toString() since: JDK1.0

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

com.sun.org.apache.xpath.internal.compiler.XPathParser.Number()

/**
   * Number ::= [0-9]+('.'[0-9]+)? | '.'[0-9]+
   *
   * @throws javax.xml.transform.TransformerException
   */
protected void Number() throws javax.xml.transform.TransformerException {
    if (null != m_token) {
        double num;
        try {
            if ((m_token.indexOf('e') > -1) || (m_token.indexOf('E') > -1)) throw new NumberFormatException();
            num = Double.valueOf(m_token).doubleValue();
        } catch (NumberFormatException nfe) {
            num = 0.0;
            error(XPATHErrorResources.ER_COULDNOT_BE_FORMATTED_TO_NUMBER, new Object[] { m_token });
        }
        m_ops.m_tokenQueue.setElementAt(new XNumber(num), m_queueMark - 1);
        m_ops.setOp(m_ops.getOp(OpMap.MAPINDEX_LENGTH), m_queueMark - 1);
        m_ops.setOp(OpMap.MAPINDEX_LENGTH, m_ops.getOp(OpMap.MAPINDEX_LENGTH) + 1);
        nextToken();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.org.apache.xerces.internal.impl.dv.xs.AbstractDateTimeDV.parseInt(String, int, int)

/**
     * Given start and end position, parses string value
     * @param value  string to parse
     * @param start  Start position
     * @param end    end position
     * @return  return integer representation of characters
     */
protected int parseInt(String buffer, int start, int end) throws NumberFormatException {
    int radix = 10;
    int result = 0;
    int digit = 0;
    int limit = -Integer.MAX_VALUE;
    int multmin = limit / radix;
    int i = start;
    do {
        digit = getDigit(buffer.charAt(i));
        if (digit < 0) throw new NumberFormatException(''' + buffer.toString() + '' has wrong format');
        if (result < multmin) throw new NumberFormatException(''' + buffer.toString() + '' has wrong format');
        result *= radix;
        if (result < limit + digit) throw new NumberFormatException(''' + buffer.toString() + '' has wrong format');
        result -= digit;
    } while (++i < end);
    return -result;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.org.apache.xerces.internal.impl.dv.xs.AbstractDateTimeDV.parseIntYear(String, int)

protected int parseIntYear(String buffer, int end) {
    int radix = 10;
    int result = 0;
    boolean negative = false;
    int i = 0;
    int limit;
    int multmin;
    int digit = 0;
    if (buffer.charAt(0) == '-') {
        negative = true;
        limit = Integer.MIN_VALUE;
        i++;
    } else {
        limit = -Integer.MAX_VALUE;
    }
    multmin = limit / radix;
    while (i < end) {
        digit = getDigit(buffer.charAt(i++));
        if (digit < 0) throw new NumberFormatException(''' + buffer.toString() + '' has wrong format');
        if (result < multmin) throw new NumberFormatException(''' + buffer.toString() + '' has wrong format');
        result *= radix;
        if (result < limit + digit) throw new NumberFormatException(''' + buffer.toString() + '' has wrong format');
        result -= digit;
    }
    if (negative) {
        if (i > 1) return result; else throw new NumberFormatException(''' + buffer.toString() + '' has wrong format');
    }
    return -result;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.math.BigInteger.parseInt(char[], int, int)

private int parseInt(char[] source, int start, int end) {
    int result = Character.digit(source[start++], 10);
    if (result == -1) throw new NumberFormatException(new String(source));
    for (int index = start; index < end; index++) {
        int nextVal = Character.digit(source[index], 10);
        if (nextVal == -1) throw new NumberFormatException(new String(source));
        result = 10 * result + nextVal;
    }
    return result;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Package.isCompatibleWith(String)

/**
     * Compare this package's specification version with a
     * desired version. It returns true if
     * this packages specification version number is greater than or equal
     * to the desired version number. 
     * Version numbers are compared by sequentially comparing corresponding
     * components of the desired and specification strings.
     * Each component is converted as a decimal integer and the values
     * compared.
     * If the specification value is greater than the desired
     * value true is returned. If the value is less false is returned.
     * If the values are equal the period is skipped and the next pair of
     * components is compared.
     * @param desired the version string of the desired version.
     * @return true if this package's version number is greater
     *   than or equal to the desired version number
     * @exception NumberFormatException if the desired or current version
     *  is not of the correct dotted form.
     */
public boolean isCompatibleWith(String desired) throws NumberFormatException {
    if (specVersion == null || specVersion.length() < 1) {
        throw new NumberFormatException('Empty version string');
    }
    String[] sa = specVersion.split('\\.', -1);
    int[] si = new int[sa.length];
    for (int i = 0; i < sa.length; i++) {
        si[i] = Integer.parseInt(sa[i]);
        if (si[i] < 0) throw NumberFormatException.forInputString('' + si[i]);
    }
    String[] da = desired.split('\\.', -1);
    int[] di = new int[da.length];
    for (int i = 0; i < da.length; i++) {
        di[i] = Integer.parseInt(da[i]);
        if (di[i] < 0) throw NumberFormatException.forInputString('' + di[i]);
    }
    int len = Math.max(di.length, si.length);
    for (int i = 0; i < len; i++) {
        int d = (i < di.length ? di[i] : 0);
        int s = (i < si.length ? si[i] : 0);
        if (s < d) return false;
        if (s > d) return true;
    }
    return true;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.spi.ior.iiop.GIOPVersion.parseVersion(String)

public static GIOPVersion parseVersion(String s) {
    int dotIdx = s.indexOf('.');
    if (dotIdx < 1 || dotIdx == s.length() - 1) throw new NumberFormatException('GIOP major, minor, and decimal point required: ' + s);
    int major = Integer.parseInt(s.substring(0, dotIdx));
    int minor = Integer.parseInt(s.substring(dotIdx + 1, s.length()));
    return GIOPVersion.getInstance((byte) major, (byte) minor);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Byte.decode(String)

/**
     * Decodes a <code>String</code> into a <code>Byte</code>.
     * Accepts decimal, hexadecimal, and octal numbers given by
     * the following grammar:
     * <blockquote>
     * <dl>
     * <dt><i>DecodableString:</i>
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
     * <dt><i>Sign:</i>
     * <dd><code>-</code>
     * </dl>
     * </blockquote>
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
     * are defined in <a href='http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282'>§3.10.1</a> 
     * of the <a href='http://java.sun.com/docs/books/jls/html/'>Java 
     * Language Specification</a>.
     * The sequence of characters following an (optional) negative
     * sign and/or radix specifier ("<code>0x</code>",
     * "<code>0X</code>", "<code>#</code>", or
     * leading zero) is parsed as by the <code>Byte.parseByte</code>
     * method with the indicated radix (10, 16, or 8).  This sequence
     * of characters must represent a positive value or a {@link
     * NumberFormatException} will be thrown.  The result is negated
     * if first character of the specified <code>String</code> is the
     * minus sign.  No whitespace characters are permitted in the
     * <code>String</code>.
     * @param     nm the <code>String</code> to decode.
     * @return   a <code>Byte</code> object holding the <code>byte</code>
     *   value represented by <code>nm</code>
     * @exception NumberFormatException  if the <code>String</code> does not
     *            contain a parsable <code>byte</code>.
     * @see java.lang.Byte#parseByte(java.lang.String, int)
     */
public static Byte decode(String nm) throws NumberFormatException {
    int radix = 10;
    int index = 0;
    boolean negative = false;
    Byte result;
    if (nm.startsWith('-')) {
        negative = true;
        index++;
    }
    if (nm.startsWith('0x', index) || nm.startsWith('0X', index)) {
        index += 2;
        radix = 16;
    } else if (nm.startsWith('#', index)) {
        index++;
        radix = 16;
    } else if (nm.startsWith('0', index) && nm.length() > 1 + index) {
        index++;
        radix = 8;
    }
    if (nm.startsWith('-', index)) throw new NumberFormatException('Negative sign in wrong position');
    try {
        result = Byte.valueOf(nm.substring(index), radix);
        result = negative ? new Byte((byte) -result.byteValue()) : result;
    } catch (NumberFormatException e) {
        String constant = negative ? new String('-' + nm.substring(index)) : nm.substring(index);
        result = Byte.valueOf(constant, radix);
    }
    return result;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Integer.decode(String)

/**
     * Decodes a <code>String</code> into an <code>Integer</code>.
     * Accepts decimal, hexadecimal, and octal numbers given
     * by the following grammar:
     * <blockquote>
     * <dl>
     * <dt><i>DecodableString:</i>
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
     * <dt><i>Sign:</i>
     * <dd><code>-</code>
     * </dl>
     * </blockquote>
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
     * are defined in <a href='http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282'>§3.10.1</a> 
     * of the <a href='http://java.sun.com/docs/books/jls/html/'>Java 
     * Language Specification</a>.
     * The sequence of characters following an (optional) negative
     * sign and/or radix specifier ("<code>0x</code>",
     * "<code>0X</code>", "<code>#</code>", or
     * leading zero) is parsed as by the <code>Integer.parseInt</code>
     * method with the indicated radix (10, 16, or 8).  This sequence
     * of characters must represent a positive value or a {@link
     * NumberFormatException} will be thrown.  The result is negated
     * if first character of the specified <code>String</code> is the
     * minus sign.  No whitespace characters are permitted in the
     * <code>String</code>.
     * @param     nm the <code>String</code> to decode.
     * @return    a <code>Integer</code> object holding the <code>int</code>
     *     value represented by <code>nm</code>
     * @exception NumberFormatException  if the <code>String</code> does not
     *            contain a parsable integer.
     * @see java.lang.Integer#parseInt(java.lang.String, int)
     * @since 1.2
     */
public static Integer decode(String nm) throws NumberFormatException {
    int radix = 10;
    int index = 0;
    boolean negative = false;
    Integer result;
    if (nm.startsWith('-')) {
        negative = true;
        index++;
    }
    if (nm.startsWith('0x', index) || nm.startsWith('0X', index)) {
        index += 2;
        radix = 16;
    } else if (nm.startsWith('#', index)) {
        index++;
        radix = 16;
    } else if (nm.startsWith('0', index) && nm.length() > 1 + index) {
        index++;
        radix = 8;
    }
    if (nm.startsWith('-', index)) throw new NumberFormatException('Negative sign in wrong position');
    try {
        result = Integer.valueOf(nm.substring(index), radix);
        result = negative ? new Integer(-result.intValue()) : result;
    } catch (NumberFormatException e) {
        String constant = negative ? new String('-' + nm.substring(index)) : nm.substring(index);
        result = Integer.valueOf(constant, radix);
    }
    return result;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Long.decode(String)

/**
     * Decodes a <code>String</code> into a <code>Long</code>.
     * Accepts decimal, hexadecimal, and octal numbers given by the
     * following grammar:
     * <blockquote>
     * <dl>
     * <dt><i>DecodableString:</i>
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
     * <dt><i>Sign:</i>
     * <dd><code>-</code>
     * </dl>
     * </blockquote>
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
     * are defined in <a href='http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282'>§3.10.1</a> 
     * of the <a href='http://java.sun.com/docs/books/jls/html/'>Java 
     * Language Specification</a>.
     * The sequence of characters following an (optional) negative
     * sign and/or radix specifier ("<code>0x</code>",
     * "<code>0X</code>", "<code>#</code>", or
     * leading zero) is parsed as by the <code>Long.parseLong</code>
     * method with the indicated radix (10, 16, or 8).  This sequence
     * of characters must represent a positive value or a {@link
     * NumberFormatException} will be thrown.  The result is negated
     * if first character of the specified <code>String</code> is the
     * minus sign.  No whitespace characters are permitted in the
     * <code>String</code>.
     * @param     nm the <code>String</code> to decode.
     * @return    a <code>Long</code> object holding the <code>long</code>
     *    value represented by <code>nm</code>
     * @exception NumberFormatException  if the <code>String</code> does not
     *            contain a parsable <code>long</code>.
     * @see java.lang.Long#parseLong(String, int)
     * @since 1.2
     */
public static Long decode(String nm) throws NumberFormatException {
    int radix = 10;
    int index = 0;
    boolean negative = false;
    Long result;
    if (nm.startsWith('-')) {
        negative = true;
        index++;
    }
    if (nm.startsWith('0x', index) || nm.startsWith('0X', index)) {
        index += 2;
        radix = 16;
    } else if (nm.startsWith('#', index)) {
        index++;
        radix = 16;
    } else if (nm.startsWith('0', index) && nm.length() > 1 + index) {
        index++;
        radix = 8;
    }
    if (nm.startsWith('-', index)) throw new NumberFormatException('Negative sign in wrong position');
    try {
        result = Long.valueOf(nm.substring(index), radix);
        result = negative ? new Long((long) -result.longValue()) : result;
    } catch (NumberFormatException e) {
        String constant = negative ? new String('-' + nm.substring(index)) : nm.substring(index);
        result = Long.valueOf(constant, radix);
    }
    return result;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Short.decode(String)

/**
     * Decodes a <code>String</code> into a <code>Short</code>.
     * Accepts decimal, hexadecimal, and octal numbers given by
     * the following grammar:
     * <blockquote>
     * <dl>
     * <dt><i>DecodableString:</i>
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
     * <dt><i>Sign:</i>
     * <dd><code>-</code>
     * </dl>
     * </blockquote>
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
     * are defined in <a href='http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282'>§3.10.1</a> 
     * of the <a href='http://java.sun.com/docs/books/jls/html/'>Java 
     * Language Specification</a>.
     * The sequence of characters following an (optional) negative
     * sign and/or radix specifier ("<code>0x</code>",
     * "<code>0X</code>", "<code>#</code>", or
     * leading zero) is parsed as by the <code>Short.parseShort</code>
     * method with the indicated radix (10, 16, or 8).  This sequence
     * of characters must represent a positive value or a {@link
     * NumberFormatException} will be thrown.  The result is negated
     * if first character of the specified <code>String</code> is the
     * minus sign.  No whitespace characters are permitted in the
     * <code>String</code>.
     * @param     nm the <code>String</code> to decode.
     * @return   a <code>Short</code> object holding the <code>short</code>
     *     value represented by <code>nm</code>
     * @exception NumberFormatException  if the <code>String</code> does not
     *            contain a parsable <code>short</code>.
     * @see java.lang.Short#parseShort(java.lang.String, int)
     */
public static Short decode(String nm) throws NumberFormatException {
    int radix = 10;
    int index = 0;
    boolean negative = false;
    Short result;
    if (nm.startsWith('-')) {
        negative = true;
        index++;
    }
    if (nm.startsWith('0x', index) || nm.startsWith('0X', index)) {
        index += 2;
        radix = 16;
    } else if (nm.startsWith('#', index)) {
        index++;
        radix = 16;
    } else if (nm.startsWith('0', index) && nm.length() > 1 + index) {
        index++;
        radix = 8;
    }
    if (nm.startsWith('-', index)) throw new NumberFormatException('Negative sign in wrong position');
    try {
        result = Short.valueOf(nm.substring(index), radix);
        result = negative ? new Short((short) -result.shortValue()) : result;
    } catch (NumberFormatException e) {
        String constant = negative ? new String('-' + nm.substring(index)) : nm.substring(index);
        result = Short.valueOf(constant, radix);
    }
    return result;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Byte.parseByte(String, int)

/**
     * Parses the string argument as a signed <code>byte</code> 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 <code>byte</code> value is returned.
     * 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>byte</code>.
     * </ul>
     * @param s  the <code>String</code> containing the 
     *   <code>byte</code>
     *                  representation to be parsed
     * @param radix the radix to be used while parsing <code>s</code>
     * @return   the <code>byte</code> value represented by the string 
     *                   argument in the specified radix
     * @exception NumberFormatException If the string does
     *                  not contain a parsable <code>byte</code>.
     */
public static byte parseByte(String s, int radix) throws NumberFormatException {
    int i = Integer.parseInt(s, radix);
    if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException('Value out of range. Value:\'' + s + '\' Radix:' + radix);
    return (byte) i;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Short.parseShort(String, int)

/**
     * Parses the string argument as a signed <code>short</code> 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 <code>byte</code> value is returned.
     * 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>short</code>.
     * </ul>
     * @param s  the <code>String</code> containing the 
     *   <code>short</code> representation to be parsed
     * @param radix the radix to be used while parsing <code>s</code>
     * @return      the <code>short</code> represented by the string 
     *              argument in the specified radix.
     * @exception NumberFormatException If the <code>String</code> 
     *   does not contain a parsable <code>short</code>.
     */
public static short parseShort(String s, int radix) throws NumberFormatException {
    int i = Integer.parseInt(s, radix);
    if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException('Value out of range. Value:\'' + s + '\' Radix:' + radix);
    return (short) i;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Integer.parseInt(String, int)

/**
     * 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. 
     * 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>
     * Examples:
     * <blockquote>
     * 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
     * </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) {
            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 {
            throw NumberFormatException.forInputString(s);
        }
    } else {
        return -result;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Long.parseLong(String, int)

/**
     * Parses the string argument as a signed <code>long</code> 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 <code>long</code>
     * value is returned.
     * Note that neither the character <code>L</code>
     * (<code>'\u004C'</code>) nor <code>l</code>
     * (<code>'\u006C'</code>) is permitted to appear at the end
     * of the string as a type indicator, as would be permitted in
     * Java programming language source code - except that either
     * <code>L</code> or <code>l</code> may appear as a digit for a
     * radix greater than 22.
     * 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 <code>radix</code> 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>long</code>. 
     * </ul>
     * Examples:
     * <blockquote>
     * parseLong('0', 10) returns 0L
     * parseLong('473', 10) returns 473L
     * parseLong('-0', 10) returns 0L
     * parseLong('-FF', 16) returns -255L
     * parseLong('1100110', 2) returns 102L
     * parseLong('99', 8) throws a NumberFormatException
     * parseLong('Hazelnut', 10) throws a NumberFormatException
     * parseLong('Hazelnut', 36) returns 1356099454469L
     * </blockquote>
     * @param      s       the <code>String</code> containing the
     *                     <code>long</code> representation to be parsed.
     * @param      radix   the radix to be used while parsing <code>s</code>.
     * @return     the <code>long</code> represented by the string argument in
     *             the specified radix.
     * @exception  NumberFormatException  if the string does not contain a
     *               parsable <code>long</code>.
     */
public static long parseLong(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');
    }
    long result = 0;
    boolean negative = false;
    int i = 0, max = s.length();
    long limit;
    long multmin;
    int digit;
    if (max > 0) {
        if (s.charAt(0) == '-') {
            negative = true;
            limit = Long.MIN_VALUE;
            i++;
        } else {
            limit = -Long.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) {
            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 {
            throw NumberFormatException.forInputString(s);
        }
    } else {
        return -result;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

Comments

Popular posts from this blog

NullPointerException

java.lang.NullPointerException NullPointerException is described in the javadoc comments as: Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object. Taking the length of null as if it were an array. Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value. Applications should throw instances of this class to indicate other illegal uses of the null object. author: unascribed version: 1.19, 12/19/03 since: JDK1.0 Where is this exception thrown? Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown. The message ' java.lang.NullPointerException: ' is thrown within the method: com.sun.corba.se.impl.interceptors.ClientRequestInfoImpl.get_r...

Connection refused: No available router to destination

This is a simple symptom-cause-solution blog entry only. I hope these blogs will help fellow administrators. Symptom The following exception occurs in WebLogic server logs. Most likely to occur during WebLogic server start-up, but similar exceptions may occur at other times. java.net.ConnectException: t3://myserver:8000: Destination unreachable; nested exception is: java.net.ConnectException: Connection refused: connect; No available router to destination] at weblogic.jndi.internal.ExceptionTranslator.toNamingException(ExceptionTranslator.java:49) at weblogic.jndi.WLInitialContextFactoryDelegate.toNamingException(WLInitialContextFactoryDelegate.java:773) at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:363) at weblogic.jndi.Environment.getContext(Environment.java:307) at weblogic.jndi.Environment.getContext(Environment.java:277) Cause This message (Connection refused: connect; No available ...

Recovering WebLogic Passwords

In one of my previous articles ( here ) I explained that the SerializedSystemIni.dat file in WebLogic contains the key used to encrypt and decrypt passwords. If you're not currently keeping this file secure I suggest you do, as with it someone can (to name a few things): Decrypt the WebLogic admin username and password from boot.properties. Recover database passwords, if JDBC Connection pools are configured, from config.xml. Recover the keystore passwords from config.xml and obtain SSL certificates stored in the jks keystores. Essentially, they can do whatever they want, so if you don't know who can read your SerializedSystemIni.dat files, look... now. In this article I will show how easy it is for this file to be used to recover lost passwords via a simple WLST script. The Script The script I use to decrypt passwords is incredibly short, and it works with WebLogic 8, 9 and 10 (probably for version 7 too). To use it, just create a new file called decryptpwd.py and paste the fol...