checking string for numbers?

is there a method that checks a string for only numbers? thanks in advance
kewelAsked:
Who is Participating?
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.

RoonaanCommented:
Code below should do the trick:

String onlynumber = "12345";

if(onlynumber.matches("\\A([0-9]+)\\z"))
{
  //only number
}

Regards

-r-
0

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
hongjunCommented:
try this

public boolean isNumeric(String s){
  try{ Float.parseFloat(s); return true; } catch (NumberFormatException e){ return false; }
}



hongjun
0
aviadbdCommented:

I would replace the pattern string to "\p{Digit}+", actually

(that would make it, ' onlynumber.matches( "\p{Digit}+" ) ')

AviadBD.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

hongjunCommented:
my above function will check for floats and integers.
For integer only,

public boolean isNumeric(String s){
  try{ Integer.parseInteger(s); return true; } catch (NumberFormatException e){ return false; }
}
0
RoonaanCommented:
hongjun, your function is in a way more sensitive to overflows than the pattern match is. Although this wouldn't be a problem to the request, it is a thing to consider.

-r-
0
DrWarezzCommented:
Go with Roonaan's solution, or someone elses. However, if you'd like to find out the positions in a string that contain a character (if any), then you could try this:

//-----8<-------------------------------------------------------------------------------------------------\\
String str = "cjfh3993uf93hf9h";
char cArr = str.toCharrArray();

String results = "";
for ( int i=0; i<cArr.length; i++ )
    if ( Character.isLetter(cArr[i]) ) results += ", i";

// Then output the results:
System.out.println( "The following char positions in the specified string are no integers: \n" +
                             results );
//-----8<-------------------------------------------------------------------------------------------------\\

HTH :)
[r.D]
0
CEHJCommented:
Do you mean 'is there a method that verifies that a String should contain only numbers'?
0
DrWarezzCommented:
If so, you could create one (if there's not already):

boolean onlyInteger( String str ) {
   private char cTemp = str.toCharArray();
   for ( int i=0; i<cTemp.length; i++ )
       if ( !(Character.isDigit(cTemp[i])) ) return false;
   return true;
}

However, hongjun's method is pretty cool. :)

[r.D]
0
CEHJCommented:
If so:

boolean allDigits = s.matches("\\d+");
0
aviadbdCommented:

About containing only numbers, the regexp should do teh trick.

Testing it against a "\p{Digit}+" does exactly that - Check that only digits occur in the string, without so much code.

AviadBD.
0
CEHJCommented:
>>without so much code

Still more than my example uses ;-)
0
aviadbdCommented:

Must agree with you on that  ;)

Better than caughting exceptions all the time, though.

Aviad
0
objectsCommented:
hongjun's suggestion is best if you want to check if the string is a valid number.
0
CEHJCommented:
>>hongjun's suggestion is best if you want to check if the string is a valid number.

No it isn't
0
RoonaanCommented:
If you define a "valid number" as "a numeric values within the boundary off a javascript int", it is.

;-)

-r-
0
hongjunCommented:
points to hongjun
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.