The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Strings of Numbers

Posted by Matt Gerrans on January 25, 2002 at 2:13 PM

> Is there a way one could check whether a value in a text file is a String or an Integer??????
> ex: is this possible ??
> If(val == String)

Well, by definition, all values in text files are strings, but if you want to see if a particular string contains only digits, you could simply try to convert it to an int; if that fails, an exception is thrown. For example:

// Assume you've already read the String s from the file:
int value = Integer.parseInt( s );
// Now, do something interesting with the int value.
catch( NumberFormatException nfe )
// Do something interesting with the string value.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us