Java Integer Create toInt(final String text, final int defaultValue)

Here you can find the source of toInt(final String text, final int defaultValue)

Description

Re-conversion of the textual representation of an Integer .

License

Open Source License

Parameter

Parameter Description
text the text to parse as integer
defaultValue value to return if the given text does not represent a valid integer value

Return

interpreted integer value

Declaration

public static int toInt(final String text, final int defaultValue) 

Method Source Code

//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;
    }
}

Related

  1. toInt(final String s)
  2. toInt(final String str, final int alt)
  3. toInt(final String str, int defaultValue)
  4. toInt(final String string)
  5. toInt(final String strPriority)
  6. toInt(final String value)
  7. toInt(float value)
  8. toInt(int byteSignificance, byte b)
  9. toInt(int[] rgb)