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  a  v  a  2s . c  om

    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;
    }
}