Perry Smith

Java Questions & Answers

Archive

Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page

Extracting Integer From A Sequential TxtFile
Wednesday, 24-Feb-99 14:11:22

137.132.124.30 writes:

How do I extract integers from a sequential txt file of the following format : 110 120 130 140...I've tried readInt()..readByte()....every possible methods in DataInputStream...but all get is some funny numbers...not 110, 120...Anyone can help?

Jay


Response
Re: Extracting Integer From A Sequential TxtFile
Thursday, 25-Feb-99 20:29:26

152.205.22.243 writes:

Warning: Untested suggestions below.

If I were attacking the problem, I'd use a loop as so:

      String strToParse;
      char chAdd = ''; // empty value
      while (chAdd != ' ') // a blank space (ASCII 32)
      {
      strToParse = strToParse + chAdd;
      chAdd = (char) disTxtFile.readByte();
      }
      try
      {
      int iNumFromFile = Integer.valueOf(strToParse);
      }
      catch (NumberFormatException nfe)
      {
      // PANIC!!! ... or do something
      }

      // do something with "iNumFromFile"
MikeD
Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page