Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (10.2k points)
How can I check if a String was a number before parsing it?

2 Answers

0 votes
by (46k points)

Using Apache Commons StringUtils.isNumeric() to verify if String is valid number

Here are a couple of examples for different input to check if String is a valid number or not, this will give you a good idea of how this method works and what you can expect from this method.

StringUtils.isNumeric(null) = false

StringUtils.isNumeric("") = false

StringUtils.isNumeric(“ '') = false

StringUtils.isNumeric("123") = true

StringUtils.isNumeric("ab2c") = false

StringUtils.isNumeric("-123") = false

StringUtils.isNumeric("+123") = false

StringUtils.isNumeric("12 3") = false

StringUtils.isNumeric("12-3") = false

StringUtils.isNumeric("12.3") = false

Just note there are a couple of interesting changes in Apache commons-lang 2.5 and 3.0, the 3.0 3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence). The Apache commons-lang 3.0 also changed empty String " " to return false and not true, which is a very important change.

Now, let's use the isNumber() method to check if String is a valid number or not

NumberUtils.isNumber(null) = false

NumberUtils.isNumber("") = false

NumberUtils.isNumber(" ") = false

NumberUtils.isNumber("123") = true

NumberUtils.isNumber("ab2c") = false

NumberUtils.isNumber("-123") = true

NumberUtils.isNumber("+123") = false

NumberUtils.isNumber("12 3") = false

NumberUtils.isNumber("12-3") = false

NumberUtils.isNumber("12.3") = true

Since Apache commons-lang 3.3 this process also supports hex 0Xhhh and octal 0ddd validation. As I said before, you can also use a regular expression to check if given String is empty or not but you need to be very careful about covering all scenarios covered by these libraries. I would not recommend you to reinvent the wheel. Even Joshua Bloch has advised in "Effective Java" that always prefer library method than writing your routine. One of the main reasons for that is testing exposure, your routing will not get the free testing by millions of programs which a library method is exposed too.

0 votes
by (33.1k points)

This is the most active way I know to check if String is Number or not.

For example:

public static boolean isNumber(String str){

  int i=0, len=str.length();

  boolean a=false,b=false,c=false, d=false;

  if(i<len && (str.charAt(i)=='+' || str.charAt(i)=='-')) i++;

  while( i<len && isDigit(str.charAt(i)) ){ i++; a=true; }

  if(i<len && (str.charAt(i)=='.')) i++;

  while( i<len && isDigit(str.charAt(i)) ){ i++; b=true; }

  if(i<len && (str.charAt(i)=='e' || str.charAt(i)=='E') && (a || b)){ i++; c=true; }

  if(i<len && (str.charAt(i)=='+' || str.charAt(i)=='-') && c) i++;

  while( i<len && isDigit(str.charAt(i)) ){ i++; d=true;}

  return i==len && (a||b) && (!c || (c && d));

}

static boolean isDigit(char c){

  return c=='0' || c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9';

}

Related questions

Browse Categories

...