• Post Reply Bookmark Topic Watch Topic
  • New Topic

location of isNumeric() ??  RSS feed

 
Adrian Airloy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been off Java for a while. But have to do some coding now. Is there an 'isNumeric()' methord in Java, if so in which class can i locate it??
I have a value being passed into a variable and need to check if it has a numerical value (and not alphabetical value) if not give it a default numerical value.
Thanks for all help.
[ January 14, 2004: Message edited by: Adrian Miranda ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For digit, use Character.isDigit(char) to check.
For other number, try Double.valueOf(String) & catch the possible NumberFormatException there.
HTH
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jumpin' Java,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a simple way to find whether a nuber numeric or not just convert that into String and then use class Double method parseDouble to get the correct numeric no.
pls chk the example

Here for a it will print the output as
This number-->a--->Is not Numeric
hopefully it will work for you.
thanks
Anurag
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The best what I have found to do this is to use the parse.. methods. I.e Integer.parseInt() or Long.parseLong() that catch the NumberFormatException.
I always wondered why Sun don't put a boolean Integer.isInt(String) method in. Any one know? I suppose it is beacuse you can just use the parseInt methods.
Chris
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Harris:

I always wondered why Sun don't put a boolean Integer.isInt(String) method in.

I've often wanted these too. Throwing an exception is a very expensive way to report "false" here. It'd be nice if they shared code with the parseXXX methods so as to guarantee they handle edge cases in the same way.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!