Convert String To Integer in safer way - Android java.lang

Android examples for java.lang:String Convert

Description

Convert String To Integer in safer way

Demo Code

import android.annotation.TargetApi;
import android.os.Build;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class Main{

    public static int safeStringToInteger(String s) {
        if (stringContentCheck(s)) {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
            }//from  www  .jav a  2s. c om
        }
        return 0;
    }
    public static boolean stringContentCheck(String s) {
        if (s != null) {
            return (s.trim().length() > 0); 
        }
        return false;
    }
}

Related Tutorials