Easy question about String

Hi!I have a string in a variable. how can I check if the String is composed just by numbers?thank you very much!
[133 byte] By [mangustaa] at [2007-10-2 6:36:05]
# 1
Do you know how to write a loop? Do you know how to get a single character by index out of a String? Do you know how to compare that character to the set of characters which are digits?Come on, do some thinking.
warnerjaa at 2007-7-16 13:38:38 > top of Java-index,Java Essentials,Java Programming...
# 2
'Numbers' (plural)? what would the separating characters be then?Does a string like "1 23 45 678 90" pass the test? Please elaborate.kind regards,Jos
JosAHa at 2007-7-16 13:38:38 > top of Java-index,Java Essentials,Java Programming...
# 3
With the help of regular expressions.Check the [url= http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#matches(java.lang.String)]matches[/url] method of String.
TimTheEnchantora at 2007-7-16 13:38:38 > top of Java-index,Java Essentials,Java Programming...
# 4
"Love Me Like You" was composed by the Magic Numbers, would that pass the test? :o)
itchyscratchya at 2007-7-16 13:38:38 > top of Java-index,Java Essentials,Java Programming...
# 5

Use the following method from Integer API

parseInt

public static int parseInt(String s)

throws NumberFormatExceptionParses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

If it throws NumberFormatException its alphanumeric else its numeric.You would however have to remove any spaces or commas etc. since they are not mumeric either

kilyasa at 2007-7-16 13:38:39 > top of Java-index,Java Essentials,Java Programming...
# 6

> Use the following method from Integer API

>

> public static int parseInt(String s)

Note to the OP however that will only work if the number represented by the string is small enough. If the string is something like "1234567889012349283472938572983562935862938562985629586", that won't work yet it still contains all numbers.

Also, parseInt will also allow for a leading '-' character, which is clearly not a "number" (digit).

warnerjaa at 2007-7-16 13:38:39 > top of Java-index,Java Essentials,Java Programming...
# 7

> > Use the following method from Integer API

> >

> > public static int parseInt(String s)

> Note to the OP however that will only work if the

> number represented by the string is small enough. If

> the string is something like

> "12345678890123492834729385729835629358629385629856295

> 86", that won't work yet it still contains all

> numbers.

> Also, parseInt will also allow for a leading '-'

> character, which is clearly not a "number" (digit).

I stand corrected.

kilyasa at 2007-7-16 13:38:39 > top of Java-index,Java Essentials,Java Programming...
# 8

> "Love Me Like You" was composed by the Magic Numbers,

> would that pass the test? :o)

In computer programming, a magic number is a special constant used for some specific purpose. It is called magic because its value or presence is inexplicable without some additional knowledge.

Dixit www.reference.com

TimTheEnchantora at 2007-7-16 13:38:39 > top of Java-index,Java Essentials,Java Programming...
# 9
Indeed. An alternative term for "magic number" being "hardcoded maintenance nightmare" :o)
itchyscratchya at 2007-7-16 13:38:39 > top of Java-index,Java Essentials,Java Programming...