Java - Write code to Parse string to Int with default value

Requirements

Write code to Parse string to Int with default value

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String source = "book2s.com";
        int def = 42;
        System.out.println(ParseInt(source, def));
    }//w ww . j  a  v  a 2s .c o m

    public static int ParseInt(String source, int def) {
        try {
            return Integer.parseInt(source);
        } catch (NumberFormatException e) {
            return def;
        }
    }
}