Java Integer Create toInt(String value, int defaultValue)

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

Description

convert the string to an integer, and return the default value if the string is null or does not contain a valid int value

License

Open Source License

Parameter

Parameter Description
value string value
defaultValue default value

Return

int

Declaration

static public int toInt(String value, int defaultValue) 

Method Source Code

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

public class Main {
    /**//from  w  ww  .  j  a v a2 s .  c o  m
     * convert the string to an integer, and return the default value if
     * the string is null or does not contain a valid int value
     *
     * @param value string value
     * @param defaultValue default value
     *
     * @return int
     */
    static public int toInt(String value, int defaultValue) {
        if (value != null) {
            try {
                return Integer.parseInt(value);
            } catch (NumberFormatException n) {
            }
        }
        return defaultValue;
    }

    /**
     * convert the string to an integer, and return 0 if
     * the string is null or does not contain a valid int value
     *
     * @param value string value
     *
     * @return int
     */
    static public int toInt(String value) {
        if (value != null) {
            try {
                return Integer.parseInt(value);
            } catch (NumberFormatException n) {
            }
        }
        return 0;
    }
}

Related

  1. toInt(String value)
  2. toInt(String value)
  3. toInt(String value)
  4. toInt(String value, int _default)
  5. toInt(String value, int def)
  6. toInt(String value, int defaultValue)
  7. toInt(String value, int defaultValue)
  8. toInt(String value, int defaultValue)
  9. toInt(String value, Integer defaultValue)