Checking if a value is numeric
Hey guys I am new to Java and have to write a java program to convert an input file to another format for loading into the company's transaction processing system.
My question is how can I check to see if a field from the input file is numeric (similar to the vb function isNumeric()).
I figure that I could simply catch the exception NumberFormatException but I just want to know if it can be done without an exception having to be thrown.
Thanks in advance for you help
Use the get*(st, null) of the type you need, e.g. Integer num = Integer.getInteger(st, null). If the conversion fails then num == null.
Thanks very much... great suggestion
I actually found a better way to do it by using the isNaN function
Cool. I'm surprised it's general enough but if it works for you that's great.
Use the Scanner class to read your input stream. You can define your own tokenizer or use the default "whitespace". You can test the datatype of the next token and decide what to do based on the datatype, and it will "valueOf" the input to the correct datatype.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL