Java Short Number Create toShort(String value, short defaultValue)

Here you can find the source of toShort(String value, short defaultValue)

Description

convert the string to an short, 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

short

Declaration

static public short toShort(String value, short defaultValue) 

Method Source Code

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

public class Main {
    /**//from  ww  w . java 2  s  .  com
     * convert the string to an short, 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 short
     */
    static public short toShort(String value, short defaultValue) {
        if (value != null) {
            try {
                return Short.parseShort(value);
            } catch (NumberFormatException n) {
            }
        }
        return defaultValue;
    }

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

Related

  1. toShort(String str, int maxLen)
  2. toShort(String str, short defaultValue)
  3. toShort(String value)
  4. toShort(String value)
  5. toShort(String value)
  6. toShort(String[] arr)