1. Using Regular Expressionsfirst, import Java.util.regex.Pattern and Java.util.regex.Matcher
/*** Use regular expressions to determine if a string is a number *@paramSTR *@return */ Public Booleanisnumeric (String str) {pattern pattern= Pattern.compile ("[0-9]*"); Matcher Isnum=Pattern.matcher (str); if( !isnum.matches ()) { return false; } return true; }
2. Functions with Java
Public Static Boolean isnumeric (String str) { for (int i = 0; i < str.length (); i++) {System.out.print ln (Str.charat (i)); if (! Character.isdigit (Str.charat (i))) { returnfalse; }} returntrue;
3. Using Org.apache.commons.lang
org.apache.commons.lang.StringUtils;BooleanIsnunicodedigits=stringutils.isnumeric ("aaa123456789"); http://jakarta.apache.org/commons/lang/api-release/index.html The following explanation: Public Static BooleanIsNumeric (String str) ChecksifThe String contains only Unicode digits. A decimal point was not a Unicode digit and returnsfalse.NULLWouldreturn false. An empty String ("") wouldreturn true. Stringutils.isnumeric (NULL) =falseStringutils.isnumeric ("") =trueStringutils.isnumeric (" ") =falseStringutils.isnumeric ("123") =trueStringutils.isnumeric ("12 3") =falseStringutils.isnumeric ("AB2C") =falseStringutils.isnumeric ("12-3") =falseStringutils.isnumeric ("12.3") =false
4. Judging ASCII code value
Public Static Boolean isNumeric0 (String str)
{
for (int i=str.length ();--i>=0;)
{ int chr=str.charat (i); if (chr<48 | | chr>57) return false ;
} returntrue;
5. Determine whether the characters in Str are 0-9
Public Static Boolean isNumeric3 (String str)
{ final String number = "0123456789"; for (int i = 0; i < number.length; i + +)
{
if (Number.indexof (Str.charat (i)) = =-1)
{
returnfalse; } } return true ;}
6. Capturing NumberFormatException anomalies
Public Static Boolean isNumeric00 (String str)
{ try{ integer.parseint (str); return true ; } Catch (NumberFormatException e)
{System.out.println ("Exception: \" "+ str +" \ "is not a number/integer ..."); returnfalse; }}
(GO) Java to determine whether a string variable is a number of six methods summary