is string a Number - Android java.lang

Android examples for java.lang:Integer

Description

is string a Number

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/* w  w w . j a v  a  2 s  . com*/
     * <pre>
     * StringUtil.isNumber(null) = false;
     * StringUtil.isNumber(&quot;&quot;) = false;
     * StringUtil.isNumber(&quot;1234&quot;) = true;
     * StringUtil.isNumber(&quot;abc123&quot;) = false;
     * </pre>
     * 
     */
    public static boolean isNumber(String str) {
        try {
            Integer.valueOf(str);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

}

Related Tutorials