Here you can find the source of getValueOfSafely(String stringToConvert, int defaultValue)
Parameter | Description |
---|---|
stringToConvert | String to convert |
defaultValue | default value |
public static int getValueOfSafely(String stringToConvert, int defaultValue)
//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; } }