Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

New String.isNumeric() function in Java 1.6 ?

1,061 views
Skip to first unread message

Ulf Meinhardt

unread,
Aug 11, 2009, 4:41:47 AM8/11/09
to
As far as I heard Sun has finally implemented something like a String.inNumeric()
function in Java 1.6 (To detect if a string is numeric).

Is his true?

If not: Is there another way to find this out WITHOUT a try catch clause like

public boolean isInteger( String input ) {
try{
Integer.parseInt( input );
return true; }
catch( Exception ) {
return false; } }

I would appreciate a one-liner.

Ulf

mirko...@googlemail.com

unread,
Aug 11, 2009, 5:08:56 AM8/11/09
to
On 11 Aug., 10:41, ul...@email.com (Ulf Meinhardt) wrote:
> As far as I heard Sun has finally implemented something like a String.inNumeric()
> function in Java 1.6 (To detect if a string is numeric).
I didn't found it.


> If not: Is there another way to find this out WITHOUT a try catch clause like
>
> public boolean isInteger( String input )  {  
>       try{  
>          Integer.parseInt( input );  
>          return true;  }  
>       catch( Exception ) {  
>          return false;  }    }
>

Try the commons-lang lib from apache.

boolean NumberUtils.isNumber(String str);
Checks whether the String a valid Java number.


Mirko

Real Gagnon

unread,
Aug 11, 2009, 8:00:59 AM8/11/09
to
ul...@email.com (Ulf Meinhardt) wrote in
news:4a812ecb$0$31332$9b4e...@newsspool4.arcor-online.net:


> I would appreciate a one-liner.

// Check if given string is a number (digits only)
public static boolean isNumber(String string) {
return string.matches("^\\d+$");
}

See http://www.rgagnon.com/javadetails/java-0599.html for more.

Bye.

--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html

Tassilo Horn

unread,
Aug 11, 2009, 7:26:33 AM8/11/09
to
ul...@email.com (Ulf Meinhardt) writes:

Hi!

> As far as I heard Sun has finally implemented something like a
> String.inNumeric() function in Java 1.6 (To detect if a string is
> numeric).
>
> Is his true?

No, there's no such method.

> If not: Is there another way to find this out WITHOUT a try catch
> clause like
>
> public boolean isInteger( String input ) {
> try{
> Integer.parseInt( input );
> return true; }
> catch( Exception ) {
> return false; } }

I think, that's the best approach (with NumberFormatException in the
catch). Another one could be done with a regular expression, like

myString.matches("^-?\d+$")

but that is not as correct as the exception check variant. For example,
the regexp would return true for numbers that are out of range for
integers, and it doesn't work with hex-values. And if a new java
version introduces another form of int literals, let's say scientific
notation like "17e22", then you need to adapt the regular expression,
while the exception checking approach would simply work.

Bye,
Tassilo

Lew

unread,
Aug 11, 2009, 8:34:48 AM8/11/09
to
Ulf Meinhardt wrote:
> As far as I heard Sun has finally implemented something like a String.inNumeric()
> function in Java 1.6 (To detect if a string is numeric).
>
> Is his true?

<http://java.sun.com/javase/6/docs/api/java/lang/String.html>

Aside from other suggestions offered here, you might find
<http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html#parse(java.lang.String)>
useful.

--
Lew

RedGrittyBrick

unread,
Aug 11, 2009, 9:45:28 AM8/11/09
to

Real Gagnon wrote:
> ul...@email.com (Ulf Meinhardt) wrote in
> news:4a812ecb$0$31332$9b4e...@newsspool4.arcor-online.net:
>
>
>> I would appreciate a one-liner.
>
> // Check if given string is a number (digits only)
> public static boolean isNumber(String string) {
> return string.matches("^\\d+$");
> }
>
> See http://www.rgagnon.com/javadetails/java-0599.html for more.
>

Real's page has better regular expressions but I suppose it is obvious
that, depending on circumstances, the following ought to be considered
valid numbers too

1,234.56
1.234,56
2e-34
0xDEADBEEF

--
RGB

markspace

unread,
Aug 11, 2009, 12:19:36 PM8/11/09
to
Ulf Meinhardt wrote:

>
> I would appreciate a one-liner.

I don't know of any one-liners, sorry. If nothing else, what do you do
if the parameter string is null? You're going to have to test for that,
at minimum.

Joshua Cranmer

unread,
Aug 11, 2009, 6:17:39 PM8/11/09
to
RedGrittyBrick wrote:
> 1,234.56
> 1.234,56
> 2e-34
> 0xDEADBEEF

None of which would pass the Integer.parseInt test as well. Granted, the
regexp curiously leaves out negative numbers...

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Arne Vajhøj

unread,
Aug 11, 2009, 7:28:12 PM8/11/09
to
Ulf Meinhardt wrote:
> As far as I heard Sun has finally implemented something like a String.inNumeric()
> function in Java 1.6 (To detect if a string is numeric).
>
> Is his true?

No.

It is not in Java docs for String.

> If not: Is there another way to find this out WITHOUT a try catch clause like
>
> public boolean isInteger( String input ) {
> try{
> Integer.parseInt( input );
> return true; }
> catch( Exception ) {
> return false; } }
>
> I would appreciate a one-liner.

If it is very rare that it is not an integer, then
just call Integer.parseInt and handle the exception
as a real exception.

If it will happen frequently that it is not an integer,
then test for it by either iterating over chars
or using regex.

Arne

Lew

unread,
Aug 11, 2009, 7:38:55 PM8/11/09
to
Joshua Cranmer wrote:
> RedGrittyBrick wrote:
>> 1,234.56
>> 1.234,56
>> 2e-34
>> 0xDEADBEEF
>
> None of which would pass the Integer.parseInt test as well. Granted, the
> regexp curiously leaves out negative numbers...

But some of them would pass the DecimalFormat.parse() test.

--
Lew

Mike Schilling

unread,
Aug 12, 2009, 4:55:25 AM8/12/09
to

I can't do a one-liner, but a numeric string would be + or - followed
by one or more digits. If you want to know whether it fits within an
int, count the digits, and if necessary do a lexical comparison with
MAX_INT.toString() or MIN_INT.toString().


RedGrittyBrick

unread,
Aug 12, 2009, 6:25:50 AM8/12/09
to

Joshua Cranmer wrote:
> RedGrittyBrick wrote:
>> 1,234.56
>> 1.234,56
>> 2e-34
>> 0xDEADBEEF
>
> None of which would pass the Integer.parseInt test as well.

Nor Double.parseDouble. I was a bit confused by the OP's use of
"isNumeric" in the subject and "isInteger" in the body.

Obviously, the answer to the OP's question depends very much on which
forms of number are expected to be encountered. This can vary greatly on
locale and context.

--
RGB

Sabine Dinis Blochberger

unread,
Sep 21, 2009, 7:28:47 AM9/21/09
to
Ulf Meinhardt wrote:


> I would appreciate a one-liner.

Here you go:

"A computer program will always do what you tell it to, and seldom what
you want it to."
--
Op3racional - www.op3racional.eu
---------------------
If you're reading this, you're on Usenet
<http://oakroadsystems.com/genl/unice.htm>

Roedy Green

unread,
Sep 21, 2009, 8:28:46 AM9/21/09
to
I see no mention of such a method in the Javadoc. Where did you hear
about it?

Normally you use parseInt and catch the NumberFormatException.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Perfect reusable components are not obtained at the first shot."
~ Bertrand Meyer (born: 1950 age: 59) 1989, creator of design by contract and the Eiffel language.

0 new messages