checking string for numbers?

is there a method that checks a string for only numbers? thanks in advance
kewelAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RoonaanConnect With a Mentor Commented:
Code below should do the trick:

String onlynumber = "12345";

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

Regards

-r-
0
 
hongjunConnect With a Mentor Commented:
try this

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



hongjun
0
 
aviadbdConnect With a Mentor Commented:

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

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

AviadBD.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
CEHJConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.