Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * convert a string to integer.
     * 
     * @param str
     * @return a int value or 0 if cant convert.
     * @see #convertStrToInteger(Object, Integer)
     */
    public static Integer convertStrToInteger(String str) {
        return convertStrToInteger(str, 0);
    }

    /**
     * convert a string to integer.
     * <p>
     * null => defaultValue<br/> "abc" => defaultValue<br/> "123" => 123<br/> "
     * 123 " => 123<br/> "1,234,567" => 1234567<br/>
     * </p>
     * 
     * @param str
     * @param defaultValue
     * @return a int value or <code>defaultValue</code> if cant convert.
     */
    public static Integer convertStrToInteger(Object str, Integer defaultValue) {
        if (null == str) {
            return defaultValue;
        }
        try {
            String s = str.toString().replace(",", "");
            return Integer.valueOf(s.trim());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static String toString(Object obj) {
        if (null == obj) {
            return "";
        }

        return obj.toString();
    }

    public static String toString(Object obj, String defaultStr) {
        if (null == obj) {
            return defaultStr;
        }

        return obj.toString();
    }
}