Java - Write code to convert String To Int

Requirements

Write code to convert String To Int

If fail, return a default value

Hint

Use Integer.parseInt method

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        System.out.println(convertStringToInt(str));
    }//from   w ww.  j  a va2 s  .  c  om

    public static int convertStringToInt(String str) {
        return convertStringToInt(str, 0);
    }

    public static int convertStringToInt(String str, int failValue) {
        if (str == null) {
            return failValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return failValue;
    }
}