Here you can find the source of toInt(final String text, final int defaultValue)
Parameter | Description |
---|---|
text | the text to parse as integer |
defaultValue | value to return if the given text does not represent a valid integer value |
public static int toInt(final String text, final int defaultValue)
//package com.java2s; /*//from ww w . j a v a 2s . co m Copyright (C) 2016 HermeneutiX.org This file is part of SciToS. SciToS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. SciToS 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 SciToS. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Re-conversion of the textual representation of an {@link Integer}. The characters in the {@code text} must all be decimal digits, except that * the first character may be an ASCII minus sign '-' to indicate a negative value. Otherwise the given {@code defaultValue} will be returned. * * @param text * the text to parse as integer * @param defaultValue * value to return if the given {@code text} does not represent a valid integer value * @return interpreted integer value */ public static int toInt(final String text, final int defaultValue) { if (text != null) { try { return Integer.parseInt(text); } catch (final NumberFormatException nfe) { // use default value instead } } return defaultValue; } }