Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# checking string for numbers?

Posted on 2004-10-23
Medium Priority
305 Views
Last Modified: 2010-03-31
is there a method that checks a string for only numbers? thanks in advance
0
Question by:kewel
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 4
• 3
• 3
• +3
19 Comments

LVL 49

Accepted Solution

Roonaan earned 500 total points
ID: 12387521
Code below should do the trick:

String onlynumber = "12345";

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

Regards

-r-
0

LVL 33

Assisted Solution

hongjun earned 500 total points
ID: 12387527
try this

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

hongjun
0

LVL 3

Assisted Solution

aviadbd earned 500 total points
ID: 12387528

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

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

AviadBD.
0

LVL 33

Expert Comment

ID: 12387529
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

LVL 49

Expert Comment

ID: 12387547
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

LVL 9

Expert Comment

ID: 12387883
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

LVL 86

Expert Comment

ID: 12387954
Do you mean 'is there a method that verifies that a String should contain only numbers'?
0

LVL 9

Expert Comment

ID: 12387968
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

LVL 86

Assisted Solution

CEHJ earned 500 total points
ID: 12387977
If so:

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

LVL 3

Expert Comment

ID: 12387978

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

LVL 86

Expert Comment

ID: 12387982
>>without so much code

Still more than my example uses ;-)
0

LVL 3

Expert Comment

ID: 12387985

Must agree with you on that  ;)

Better than caughting exceptions all the time, though.

Aviad
0

LVL 92

Expert Comment

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

LVL 86

Expert Comment

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

No it isn't
0

LVL 49

Expert Comment

ID: 12392817
If you define a "valid number" as "a numeric values within the boundary off a javascript int", it is.

;-)

-r-
0

LVL 33

Expert Comment

ID: 12632402
points to hongjun
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be uâ€¦
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Javaâ€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
###### Suggested Courses
Course of the Month9 days, 23 hours left to enroll

#### 610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.