Parse Short from String with default value - Java java.lang

Java examples for java.lang:String Parse

Description

Parse Short from String with default value

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String value = "java2s.com";
        System.out.println(getShort(value));
    }//from w w w  . j  a v  a2 s.c  om

    public static Short getShort(String value, Short defaultValue) {
        try {
            return Short.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Short getShort(String value) {
        return getShort(value, null);
    }
}

Related Tutorials