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
> 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
> 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
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
<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
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
>
> 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.
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
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
But some of them would pass the DecimalFormat.parse() test.
--
Lew
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().
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
> 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>
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.