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

Java examples for java.lang:String Parse

Description

Parse Integer 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(getInt(value));
    }/*from   w w w . java  2s  .  co m*/

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

    public static Integer getInt(String value) {
        return getInt(value, null);
    }
}

Related Tutorials