Parse string to short, return defaultValue if parse fails - Java java.lang

Java examples for java.lang:String Parse

Description

Parse string to short, return defaultValue if parse fails

Demo Code



public class Main{
    /** Parse string to short, return defaultValue if parse fails */
    public static short parseShort(String value, short defaultValue) {
        short parsed = defaultValue;
        if (value != null) {
            try {
                parsed = Short.parseShort(value);
            } catch (NumberFormatException e) {
                parsed = defaultValue;/* ww w.  j  ava2  s. co m*/
            }
        }
        return parsed;
    }
}

Related Tutorials