Martin Baker wrote:

> My question is: what is the most efficient way to read in large arrays of
> numbers? (such as a very big IndexedFaceSet)

> I get the impression that the most 'architecturally sound' way to do
> formatted I/O is to use the java.text package.

Not really. I only ever use this when writing data out, not reading.

> So I tend to read the file one line at a time, use StringTokenizer to split
> it up, then convert to a double floating point number using something like:
>
> value = (new Double(s)).doubleValue();

Yuck. You are better off using StreamTokeniser (in java.io) and just
splitting it based on the separator char - assuming linefeeds are not
important. Here is one method of doing it:

InputStream is = // however you open it.

double[] data = new double[some_size];

StreamTokenizer strtok = new StreamTokenizer(is);
strtok.eolIsSignificant(false);
strtok.parseNumbers();
strtok.whitespaceChars(',');  // set up comma delimited if needed
strtok.commentChar('#');      // set up # as a comment char if needed

int i = 0;
int type;

while((type = strtok.nextToken()) != StringTokenizer.TT_EOF)
{
  switch(type)
  {
    case StringTokenizer.TT_NUMBER:
      data[i++] =  strtok.nval;
      break;

    case StringTokenizer.TT_WORD:
      // do something or toss it away
      break;

    case .....

  }
}


Alternatively, there is also a static parseDouble() method for the
String. This saves on allocation.

try
{
  double dbl = Double.parseDouble(s);
}
catch(NumberFormatException nfe)
{
}

--
Justin Couch                                   Author, Java Hacker
Snr Software Engineer                     [EMAIL PROTECTED]
ADI Ltd, Systems Group              http://www.vlc.com.au/~justin/
Java3D FAQ:       http://tintoy.ncsa.uiuc.edu/~srp/java3d/faq.html
-------------------------------------------------------------------
"Look through the lens, and the light breaks down into many lights.
 Turn it or move it, and a new set of arrangements appears... is it
 a single light or many lights, lights that one must know how to
 distinguish, recognise and appreciate? Is it one light with many
 frames or one frame for many lights?"      -Subcomandante Marcos
-------------------------------------------------------------------

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to