check if a string is a number

hi all

I am currently using this method to check if the string passed is actually a number value.

	public static boolean isInteger(String s) {
	    int radix = 10;
	    if(s.isEmpty()) return false;
	    for(int i = 0; i < s.length(); i++) {
	        if(i == 0 && s.charAt(i) == '-') {
	            if(s.length() == 1) return false;
	            else continue;
	        if(Character.digit(s.charAt(i),radix) < 0) return false;
	    return true;

Open in new window

But it seems like performance wise its not very good since it iterates through each character and checks if it is numeric or not[ O(n)].
I would like to know if there is a better way to do it ?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The check needs to be O(n) for an n-digit input because the input could be:


The only way to find the invalid character (the # in this case) is to check each character (as it could be in any position) - which is O(n).

So what you have is an optimum solution (at least in big-O terms).

If you'd like less code - just pass it to Integer.parse(s) - it'll throw if it's not valid.  But internally it's still O(n).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gerwin Jansen, EE MVETopic Advisor Commented:
Don't use a loop to check, takes way to much time. Use a try/catch block around a parse. If you get an exception it's not a number.

<edit> Didn't see your comment Doug
Doing a try catch block will work however I'm thinking it probably breaks item 57 from effective java - using exceptions to control the flow of your application.

Can it not be done using a regex? ....i'm not aware of the Big-O complexity of using a regex. Perhaps Doug can comment?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

The regex approach can't be better than O(n).

Here's another way to see that.

Imagine the regex is super clever, building a super efficient piece of code that somehow doesn't examine each character in the input string.  E.g. Given a string of 10 characters, it only looks at 9 of them.  So it's less than O(n).  But then I can give it an input string with a # in that position it didn't look at - and the regex won't work correctly (since it never examined that char).

So O(n) is the best possible - no matter what code is used.

awking00Information Technology SpecialistCommented:
public static boolean isInteger(String s) {
      return Pattern.matches("[0-9]+",s);
public static boolean isInteger(String s) {
      return Pattern.matches("[0-9]+",s);

I'm not great at regexs, but I think that's isPositiveInteger.

awking00Information Technology SpecialistCommented:
Good catch, Doug. To accommodate negative numbers, it should be -
public static boolean isInteger(String s) {
       return Pattern.matches("-?[0-9]+",s);
royjaydAuthor Commented:
Thanks all.
Think this was pretty fully covered
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.