# checking string for numbers?

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

Commented:
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

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

Commented:
try this

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

hongjun
0
Commented:

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

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

0
Commented:
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
Commented:
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
Commented:
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
Commented:
Do you mean 'is there a method that verifies that a String should contain only numbers'?
0
Commented:
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
Commented:
If so:

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

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.

0
Commented:
>>without so much code

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

Must agree with you on that  ;)

Better than caughting exceptions all the time, though.

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

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

;-)

-r-
0
Commented:
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.