Checking if a value is numeric


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Checking if a value is numeric

  1. #1
    Join Date
    Jun 2005
    Posts
    3

    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

  2. #2
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    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.

  3. #3
    Join Date
    Jun 2005
    Posts
    3
    Thanks very much... great suggestion

  4. #4
    Join Date
    Jun 2005
    Posts
    3
    I actually found a better way to do it by using the isNaN function

  5. #5
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Cool. I'm surprised it's general enough but if it works for you that's great.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468

    Use Scanner

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center