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  w w  .java2s . c  o m*/

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

Related Exercise