> Hello folks. I've had a problem with the parsing part of HW1 and it seems
> this will continue to be a problem. My basic difficulty centers around the
> transition between using the strings that StringTokenizer would generate
> and the subsequent conversion and storage of these string values as usable
> integers.
> 
> // for exampe, this is what I would invision trying
> 
> BufferedReader input = new BufferedReader ( new FileReader (filename));
> StringTokenizer example = new StringTokenizer( input);
> 
> // try to load tokens into an int array
> 
> for (  conditions )
> {
>    data[ i ] = example.nextToken();
> }

The JavaDoc for StringTokenizer.nextToken() shows that it returns a 
String.  Assuming you declared "int data[]", you need to put ints into 
each element of data[].  How to convert a String to an int:

  try {
      String token = example.nextToken();
      data[i] = Integer.parseInt(token);
  } catch ( NumberFormatException nfe ) {
      // Handle the error here.
  }

Never used StringTokenizer before?  Never heard of Integer with a 
capital I?  What's this NumberFormatException thing?  Now's your chance 
to read the JavaDoc that came with your JDK or IDE.  Every time you 
encounter a class that's new to you, read its JavaDoc.  This is the best 
way for you and the new class to become good friends.

-- 
Richard Kasperowski (mailto:[EMAIL PROTECTED])
Tel: 617-576-1552, Fax: 617-576-2441
http://www.altisimo.com/


Reply via email to