Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a String is numeric in Java

How would you check if a String was a number before parsing it?

like image 287
Craig Angus Avatar asked Jul 09 '09 09:07

Craig Angus


People also ask

Is string a number Java?

Hence, In the Java application, the simplest way to determine if a String is a number or not is by using the Apache Commons lang's isNumber() method, which checks whether the String is a valid number in Java or not.

How do you check if a string is just numbers?

Use the test() method to check if a string contains only digits, e.g. /^[0-9]+$/. test(str) . The test method will return true if the string contains only digits and false otherwise. Copied!

How do you check if a string is an integer?

The isdigit() method is an attribute of the string object to determine whether the string is a digit or not. This is the most known method to check if a string is an integer. This method doesn't take any parameter, instead, it returns True if the string is a number (integer) and False if it's not.


2 Answers

This is generally done with a simple user-defined function (i.e. Roll-your-own "isNumeric" function).

Something like:

public static boolean isNumeric(String str) {    try {       Double.parseDouble(str);       return true;   } catch(NumberFormatException e){       return false;     }   } 

However, if you're calling this function a lot, and you expect many of the checks to fail due to not being a number then performance of this mechanism will not be great, since you're relying upon exceptions being thrown for each failure, which is a fairly expensive operation.

An alternative approach may be to use a regular expression to check for validity of being a number:

public static boolean isNumeric(String str) {   return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal. } 

Be careful with the above RegEx mechanism, though, as it will fail if you're using non-Arabic digits (i.e. numerals other than 0 through to 9). This is because the "\d" part of the RegEx will only match [0-9] and effectively isn't internationally numerically aware. (Thanks to OregonGhost for pointing this out!)

Or even another alternative is to use Java's built-in java.text.NumberFormat object to see if, after parsing the string the parser position is at the end of the string. If it is, we can assume the entire string is numeric:

public static boolean isNumeric(String str) {   ParsePosition pos = new ParsePosition(0);   NumberFormat.getInstance().parse(str, pos);   return str.length() == pos.getIndex(); } 
like image 140
CraigTP Avatar answered Sep 27 '22 19:09

CraigTP


With Apache Commons Lang 3.5 and above: NumberUtils.isCreatable or StringUtils.isNumeric.

With Apache Commons Lang 3.4 and below: NumberUtils.isNumber or StringUtils.isNumeric.

You can also use StringUtils.isNumericSpace which returns true for empty strings and ignores internal spaces in the string. Another way is to use NumberUtils.isParsable which basically checks the number is parsable according to Java. (The linked javadocs contain detailed examples for each method.)

like image 34
palacsint Avatar answered Sep 27 '22 19:09

palacsint