Convert String to Int with default value - Android java.lang

Android examples for java.lang:String Convert

Description

Convert String to Int with default value

Demo Code

public class Main{

    public static int toInt(String str, int defValue) {
        try {/*from w  w w .j  av  a2  s  .co  m*/
            return Integer.parseInt(str.replace("+", ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defValue;
    }

    public static int toInt(Object obj) {
        if (obj == null)
            return 0;
        return toInt(obj.toString(), 0);
    }

}

Related Tutorials