Java Integer Parse tryParseInt(Object o)

Here you can find the source of tryParseInt(Object o)

Description

Given an Object that may be null or may be an Integer, this method attempts to convert the value to an Integer.

License

Apache License

Parameter

Parameter Description
o the object to try to convert

Return

the converted value, if o is an Integer; null otherwise

Declaration

public static Integer tryParseInt(Object o) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from   ww  w  . j a v  a2 s .c  o m*/
     * Given an <code>Object</code> that may be null or may be an Integer, this
     * method attempts to convert the value to an <code>Integer</code>. If successful,
     * the <code>Integer</code> value is returned; otherwise, <code>null</code> is returned.
     * NOTE: the [non-null] object is first converted to a string and is trimmed of whitespace.
     * @param o the object to try to convert
     * @return the converted value, if <em>o</em> is an <code>Integer</code>; null otherwise
     */
    public static Integer tryParseInt(Object o) {
        if (o == null)
            return null;

        Integer retVal = null;
        try {
            retVal = Integer.parseInt(o.toString().trim());
        } catch (NumberFormatException nfe) {
        }

        return retVal;
    }
}

Related

  1. tryParseInt(Object obj, Integer defaultVal)
  2. tryParseInt(String intString, int defaultValue)
  3. tryParseInt(String num)
  4. tryParseInt(String s)