Java - Write code to convert String To Long

Requirements

Write code to convert String To Long

If fail return a default value

Hint

Use Long.parseLong() method

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        long failValue = 42;
        System.out.println(convertStringToLong(str, failValue));
    }//  w  ww. j ava  2 s.  com

    public static long convertStringToLong(String str, long failValue) {
        if (str == null) {
            return failValue;
        }
        try {
            return Long.parseLong(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return failValue;
    }
}