Java - Write code to Try to Parse a string to int

Requirements

Write code to Try to Parse a string to int

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String value = "book2s.com";
        int defaultValue = 42;
        System.out.println(TryParse(value, defaultValue));
    }// www  .  j  a  v a  2s  .  c  o m

    public static int TryParse(String value, int defaultValue) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return defaultValue;
        }
    }
}