checking string for digits

if i have a string is there a way to check that each character in the string is a digit

i know :

java.lang.Character.isDigit(stringX.charAt(3))

will show true if the 4th character (at position 3 in the index) is a digit

this is similar to what i want but the length of the string can be any size and im not sure how to check each character to see if its a digit if i dont know the size of the string

[431 byte] By [chiasa] at [2007-11-27 3:55:39]
# 1
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlOr a simple for loop over each character.
CeciNEstPasUnProgrammeura at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 2
Or you could place the Integer.parseInt method call inside a try/catch statement.
floundera at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 3
At least Long.parseLong, since that has a broader range. If you got a long phone number, parseint might fail. For some other numbers, parseLong maybe, too.
CeciNEstPasUnProgrammeura at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 4
True! I based my answer on previous question by OP where they were trying to use parseInt.
floundera at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 5
boolean containsOnlyNumeric = string.matches("\\d+");
sabre150a at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 6

> Or you could place the Integer.parseInt method call

> inside a try/catch statement.

out of interest what is a try/catch method?

>Or a simple for loop over each character

if i did a loop, would it be something like:

int index = 0;

boolean found = false;

while (index < stringX.size() && !found)

{

String stringX = stringX.get(index);

if (java.lang.Character.isDigit(stringX))

{

found = true;

}

else

{

index++;

}

}

do u think thats right

(and thnx flounder for ur help in the other the topic)

chiasa at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 7
Why do people post code and ask us if it is correct?Better solution is to read the API and determine what parameters the methods take and what values they return. Or the very least try and compile it.
floundera at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 8
> boolean containsOnlyNumeric = string.matches("\\d+");does this search a string of any size, for only digits and what class does this come under? string?
chiasa at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 9

>Why do people post code and ask us if it is correct?

>

>Better solution is to read the API and determine what parameters the methods take and what values they return. Or the very least try and compile it.

o ok, thats fair - sorry

Message was edited by:

chias

chiasa at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 10

> > boolean containsOnlyNumeric =

> string.matches("\\d+");

>

> does this search a string of any size,

YES!

> for only

> digits and what class does this come under? string?

Yes 'String' but behind the scenes it uses the regular expression library java.util.regex.*.

sabre150a at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...
# 11
> > > boolean containsOnlyNumeric => > string.matches("\\d+");> Yes 'String' but behind the scenes it uses the> regular expression library java.util.regex.*.thnx
chiasa at 2007-7-12 8:59:50 > top of Java-index,Java Essentials,New To Java...