Link to home
Start Free TrialLog in
Avatar of kewel
kewel

asked on

Checking strings for a only numbers

I need to know if there is a method associated with a class that simply checks wheter a string contains only numbers(these numbers are 0-7). If not, can someone post some code that will tell me how check a string for a value of 0-7? Thanks in advance!


ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How about this:

public boolean isNumber()
{
 try {
   Integer.valueOf(str)
   return true;
 } catch (NumberFormatException x)
   return false;
 }
}
this will not do it MogalManic because not all numbers are allowed just from 0 to 7, right kewel?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gnudiff
gnudiff

Regexps are generally very good, I agree.

However, if the task is simply to check for digits 0-7, then using a regexp is an overkill; I have an (untested) suspicion that a regexp is a magnitude more resource intensive than a parseInt().
I have to disagree with gnudiff. I've used regexp quite often and the power they bring is NOT at the expense of overhead (in my opinion admittedly). These pieces of code are optimised so that they take the smartest route to finding the solution and as a rule it's always better to take advantage of the standard api methods rather than role your own. They are also easier to extend and maintain . Imagine if the string gets changed so that it gets pre-fixed with 3 lowercase alphabetic charater, due to expansion requirements, the regexp changes from "([0-7]*)" to "([a-z]{3}[0-7]*)", which is hardly tough.

It is the fact that they are so much more complex to write the first time you use one tends to put people off, but to be honest, go with whatever you feel most comfortable with as it's what you'll use again and again.
Well, it is the specifics of the case.

Whenever we are talking general string manipulation, there can be tons of ways of using a regexp effectively. And there I would agree with you completely. I like regexps myself, and a regexp is frequently (due to internal optimization) better than lots of String functions.

However, I will have a hard time believing the regexp engine itself could be more efficient than a simple internal

int i=Integer.parseInt(str, 8);

If it is, then I think the Java engine is in serious need of optimization.