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

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

Description

Method to convert string to Integer when a default value is given

License

Open Source License

Parameter

Parameter Description
str a parameter
defaultValue a parameter

Return

int

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w w w . ja va  2 s . c om
     * Method to convert string to Integer
     * 
     * @param str
     * @return {@link int}
     */
    public static int toInt(final String str) {
        return toInt(str, 0);
    }

    /**
     * Method to convert string to Integer when a default value is given
     * 
     * @param str
     * @param defaultValue
     * @return {@link int}
     */
    public static int toInt(final String str, int defaultValue) {
        if (str == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (final NumberFormatException exception) {
            return defaultValue;
        }
    }
}

Related

  1. toInt(final long n)
  2. toInt(final Object obj)
  3. toInt(final Object valueRep)
  4. toInt(final String s)
  5. toInt(final String str, final int alt)
  6. toInt(final String string)
  7. toInt(final String strPriority)
  8. toInt(final String text, final int defaultValue)
  9. toInt(final String value)