In day-to-day programming, you often need to check if a given string is numeric or not. It's also a good interview question but that's a separate topic of discussion. Even though you can use Regular expression to check if the given String is empty or not, as shown here, they are not full proof to handle all kinds of scenarios, which common third-party libraries like Apache commons-lang will handle e.g. hexadecimal and octal String. 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.
Valid numbers include hexadecimal marked with the 0x or 0X qualifier, octal numbers, scientific notation, and numbers marked with a type qualifier (e.g. 123L). Non-hexadecimal strings beginning with a leading zero are treated as octal values.
Thus the string 09 will return false since 9 is not a valid octal value. However, numbers beginning with 0. are treated as decimal.null and empty/blank String will return false.
Another way to check if String is numeric or not is by using the isNumeric() method of StringUtils class from the same library. This method also has rules to determine valid numbers e.g. it checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.
Similarly, this method will return false for null and empty String. It also does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt() or Long.parseLong(), e.g. if the value is outside the range of int or long respectively.
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
Since Apache commons-lang 3.3 this method also supports hex 0Xhhh and octal 0ddd validation. As I said before, you can also use a regular expression to check if the given String is empty or not but you need to be very careful about covering all scenarios covered by these libraries.
Personally, I would not recommend you reinvent the wheel. Even Joshua Bloch has advised in "Effective Java" that always prefer the library method to writing your own routine.
One of the main reasons for that is testing exposure, your own routing will not get the free testing by millions of programmers to which a library method is exposed.
That's all about how to check if String is a valid number in Java or not. Though you can write your own routine in Java to find out whether a String is numeric or not, it will require you to handle a lot of special cases e.g. leading + or - sign, out-of-range integers, decimal points, octal or hexadecimal numbers, etc.
So if you don't have your requirement locked down, it's better not to reinvent the wheel and reuse the tried and tested code from the popular open-source libraries like Apache Commons lang. Even Josh Bloch advised about knowing and using library methods for better code.
Related Java String tutorials for Programmers
Valid numbers include hexadecimal marked with the 0x or 0X qualifier, octal numbers, scientific notation, and numbers marked with a type qualifier (e.g. 123L). Non-hexadecimal strings beginning with a leading zero are treated as octal values.
Thus the string 09 will return false since 9 is not a valid octal value. However, numbers beginning with 0. are treated as decimal.null and empty/blank String will return false.
Another way to check if String is numeric or not is by using the isNumeric() method of StringUtils class from the same library. This method also has rules to determine valid numbers e.g. it checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.
Similarly, this method will return false for null and empty String. It also does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt() or Long.parseLong(), e.g. if the value is outside the range of int or long respectively.
Using Apache Commons StringUtils.isNumeric() to check 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 work 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 method also supports hex 0Xhhh and octal 0ddd validation. As I said before, you can also use a regular expression to check if the given String is empty or not but you need to be very careful about covering all scenarios covered by these libraries.
Personally, I would not recommend you reinvent the wheel. Even Joshua Bloch has advised in "Effective Java" that always prefer the library method to writing your own routine.
One of the main reasons for that is testing exposure, your own routing will not get the free testing by millions of programmers to which a library method is exposed.
That's all about how to check if String is a valid number in Java or not. Though you can write your own routine in Java to find out whether a String is numeric or not, it will require you to handle a lot of special cases e.g. leading + or - sign, out-of-range integers, decimal points, octal or hexadecimal numbers, etc.
So if you don't have your requirement locked down, it's better not to reinvent the wheel and reuse the tried and tested code from the popular open-source libraries like Apache Commons lang. Even Josh Bloch advised about knowing and using library methods for better code.
Related Java String tutorials for Programmers
- How to format a String in Java? (tutorial)
- How to parse String to float in Java? (tutorial)
- How to compare two String in Java? (tutorial)
- How to replace String in Java? (example)
- How to convert char to String in Java? (tutorial)
- How to add leading zeros to an Integer in Java? (tutorial)
- How to join String by a delimiter in Java 8? (tutorial)
- How to split String by delimeter in Java? (tutorial)
- How to concatenate String in Java? (example)
- How to split a String by whitespace or tabs in Java? (tutorial)
- How to convert String to Date in Java? (tutorial)
- When should you use the intern() method of String? (article)
No comments:
Post a Comment