Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is String number?

 
Pavel Kubal
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to check if given String variable is number?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple approaches.
1) You can look at the string and see if it looks like a number. Regular expressions could match digits and decimal points and signs and all those things. If you're pre JDK 1.4 and don't have regular expressions, you can inspect each character. You'd pretty much have to duplicate all the syntax rules of Java numbers to handle all numeric types.
2) Use one of the numeric types to parse the String, like Integer.parseInt(String). If you feed it non numeric data, or data out of range or otherwise unsuitable to the type, you'll get an exception to let you know. You can do this in a few lines without knowing the syntax and ranges and so on, because the JDK will do all the checking for you.
With either approach, consider creating a utility class to house the logic. "Utility" here is an unofficial OO term for classes with all static methods. You'll be able to use it like this for years to come:
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a note, option #1 is more efficient that option #2, which can take a thousand times as long to determine that a string is not a number. When your talking one option taking 100 milliseconds instead of .1 millisecond, your gain is not much from a human perspective. When your doing 100,000 of these checks, however, the time can add up (10,000 seconds as opposed to 10 seconds).
 
Pavel Kubal
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..and how can I do it without any exception, which can stop executing my program?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 1 uses no exceptions. Option 2 throws an exception, but you can catch it, and prevent it from stopping your program. Since this is posted in Beginner, I suspect that option 2 is best for you - you need to study how to use try/catch loops to catch the exception, as this is a standard technique of Java programming. Option 1 is a more advanced technique, in my opinion.
 
Pavel Kubal
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good, can you post few lines of code of option 1? I'm not sure how can I do it. Thanks in advance.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're interested to explore the regular expression approach, then I'd suggest you might like to read an introductory article to the subject that I wrote.
If you're interested to create a solution without the use of the java.util.regex API, then I'd suggest taking a look at the J2SE API Documentation for some of the methods in the String and Characters classes. In the String class, you might make use of the charAt method. In the Character class, you might find something that helps to determine if a particular character is a digit.
Get started, and folks around here will be happy to nudge you along in the right direction.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's kind of a 3rd approach I used in COBOL (!) in the previous century. It's a bit of pattern matching plus a bit of converting from character to number.

When done, you have either a number (say an int) or an error message, never both. I left to your imagination how to convert a char to a number 0-9. Hint: it's easy. You'd want some range checking to make sure you don't exceed Integer.MAX_VALUE or MIN_VALUE.
For fancy datatypes that have decimal points, floating bits, etc things get harder. RegEx is a neater way to check syntax for those.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HTH,
M
[ April 20, 2004: Message edited by: Max Habibi ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic