Java Integer Create toInteger(Object _value, int _default)

Here you can find the source of toInteger(Object _value, int _default)

Description

Converts a string to an integer, and if unable to do so, returns the default value

License

Open Source License

Parameter

Parameter Description
_value a parameter
_default a parameter

Declaration

public static int toInteger(Object _value, int _default) 

Method Source Code

//package com.java2s;
/* // ww w. j  a  v  a 2s.  com
 *  Copyright (C) 2011,2012 AW2.0 Ltd
 *
 *  org.aw20 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  Free Software Foundation,version 3.
 *  
 *  OpenBD is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with org.aw20.  If not, see http://www.gnu.org/licenses/
 *  
 *  Additional permission under GNU GPL version 3 section 7
 *  
 *  If you modify this Program, or any covered work, by linking or combining 
 *  it with any of the JARS listed in the README.txt (or a modified version of 
 *  (that library), containing parts covered by the terms of that JAR, the 
 *  licensors of this Program grant you additional permission to convey the 
 *  resulting work. 
 *  
 *  $Id: StringUtil.java 3517 2012-12-19 16:33:25Z andy $
 */

public class Main {
    /**
     * Converts a string to an integer, and if unable to do so, returns the default value
     * 
     * @param _value
     * @param _default
     * @return
     */
    public static int toInteger(Object _value, int _default) {
        if (_value == null)
            return _default;
        else if (_value instanceof Integer)
            return (Integer) _value;
        else if (_value instanceof Long)
            return ((Long) _value).intValue();
        else if (_value instanceof Double)
            return ((Double) _value).intValue();
        else if (_value instanceof Float)
            return ((Float) _value).intValue();
        else
            return toInteger(_value.toString(), _default, 10);
    }

    public static int toInteger(String _value, int _default) {
        return toInteger(_value, _default, 10);
    }

    public static int toInteger(String _value, int _default, int _radix) {
        if (_value == null || _value.length() == 0)
            return _default;
        else {
            try {
                return Integer.parseInt(_value, _radix);
            } catch (Exception e) {
                return _default;
            }
        }
    }

    /**
     * Tries to cast Object to String. If object is null, it will return null.
     * 
     * @param _o
     * @return String value of _o
     */
    public static String toString(Object _o) {
        return toString(_o, null);
    }

    /**
     * Tries to cast Object to String. If object is null, it will return the default value supplied..
     * 
     * @param _o
     * @param _default
     * @return String value of _o
     */
    public static String toString(Object _o, String _default) {
        if (_o == null) {
            return _default;
        } else {
            try {
                return (String) _o;
            } catch (ClassCastException e) {
                return _default;
            }
        }
    }
}

Related

  1. toInteger(long n)
  2. toInteger(Number n)
  3. toInteger(Number n)
  4. toInteger(Number number)
  5. toInteger(Object _inStrObj)
  6. toInteger(Object anObj)
  7. toInteger(Object cell)
  8. toInteger(Object o)
  9. toInteger(Object o)