The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2001

Advertisement

Advertisement

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.

Message:

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:
try
{
int value = Integer.parseInt( s );
// Now, do something interesting with the int value.
}
catch( NumberFormatException nfe )
{
// Do something interesting with the string value.
}





Replies:

Sponsored Links



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