This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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:
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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).
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Mar 13, 2004
..and how can I do it without any exception, which can stop executing my program?
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.
"I'm not back." - Bill Harding, Twister
Joined: Mar 13, 2004
Good, can you post few lines of code of option 1? I'm not sure how can I do it. Thanks in advance.
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.
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.