Android String to Int Convert getValueOfSafely(String stringToConvert, int defaultValue)

Here you can find the source of getValueOfSafely(String stringToConvert, int defaultValue)

Description

Converts stringToConvert to int safely

Parameter

Parameter Description
stringToConvert String to convert
defaultValue default value

Return

stringToConvert value converted to int or defaultValue if conversion impossible

Declaration

public static int getValueOfSafely(String stringToConvert,
        int defaultValue) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w  w  w  .  j av a2 s .  c  o m*/
     * Converts {@code stringToConvert} to {@code int} safely
     * 
     * @param stringToConvert
     *            {@code String} to convert
     * @param defaultValue
     *            default value
     * @return {@code stringToConvert} value converted to {@code int} or
     *         {@code defaultValue} if conversion impossible
     */
    public static int getValueOfSafely(String stringToConvert,
            int defaultValue) {
        if (null == stringToConvert) {
            return defaultValue;
        }

        int convertedValue = 0;
        try {
            convertedValue = Integer.valueOf(stringToConvert);
        } catch (NumberFormatException ex) {
            return defaultValue;
        }
        return convertedValue;
    }
}

Related

  1. parseInt(String s)
  2. isInt(final String chaineToTest)
  3. isInteger(String string)
  4. toInt(String str, int defValue)
  5. toInt(Object obj)
  6. isNullOrWhiteSpace(final String string)
  7. isNum(String s)