Java String to Int asInt(String value)

Here you can find the source of asInt(String value)

Description

Returns the integer corresponding to the given string value .

License

Apache License

Parameter

Parameter Description
value The String value.

Return

the integer corresponding to the given string value or null if the value does not contain a parsable integer.

Declaration

public static Integer asInt(String value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  ww w .  j a  va  2  s .co m*/
     * Returns the integer corresponding to the given string {@code value}.
     *
     * @param value
     *         The {@link String} value.
     * @return the integer corresponding to the given string {@code value} or {@code null} if the {@code value} does not
     * contain a parsable integer.
     */
    public static Integer asInt(String value) {
        return asInt(value, null);
    }

    /**
     * Returns the integer corresponding to the given string {@code value}.
     *
     * @param value
     *         The {@link String} value.
     * @param defaultValue
     *         The default value returned by the method if the given {@code value} is not a valid integer.
     * @return the integer corresponding to the given string {@code value} or {@code defaultValue} if the {@code value}
     * does not contain a parsable integer.
     */
    public static Integer asInt(String value, Integer defaultValue) {
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}

Related

  1. asInt(String number)
  2. asInt(String str)
  3. asInt(String str)
  4. asInt(String str, int defaultValue)
  5. asInt(String sval, int dflt)
  6. asInteger(String param)
  7. asInteger(String string)
  8. asInteger(String v)
  9. asInteger(String value)