reading doubles from a file

How would I go about reading doubles from a file? Suppose the following as a sample file:

5 4
0.30 0.20 0.10 0.40
0.05 0.15 0.25 0.55
0.30 0.20 0.10 0.40
0.05 0.15 0.25 0.55
0.30 0.20 0.10 0.40

The first line would have two integers -- how many lines there are, and how many doubles were in each line.
Then there would be the specified number of lines and each would have the specified number of doubles (each double would be separated from the other by a space or tab).  

I would like to read the first line into two integer variables, and then the rest of the file into an array (or a vector, but only if it's a must) whose size is determined by the given values.

Thanks
cokeman_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cokeman_Author Commented:
actually, change that -- it doesn't have to be doubles that are read in -- integers are fine too
0
beyondrobCommented:
You could read eachline in as a string, then use a string tokenizer to get at each value which can be converted to doubles or ints or whatever through methods in each class (Integer, Double, etc)

I have code somewhere to do most of this if you're interested
0
cokeman_Author Commented:
that would be good.  if possible, could you give me the code to read the lines too? I'm new to file handling in Java and personally, I think it seems to be a pain in the butt =)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

jpk041897Commented:
You might also want to look at DataInputStream to avoid the use of tokenizers. Additionaly, the use of DataInputStream could be built ontop of a BufferedInputStream to increase performance if you are using large files.
0
dufortCommented:
/*ANSWER
Here's my solution. I don't know if it's the best in town but, eh, it worked with your sample file. I read the complete file and jam it in a String, then tokenize it and process it token by token. Fun and simple. Could be adapted to anything. Well almost.

It's for JDK 1.1.x of course. If you have 1.0.x, I guess you will have to change the small part where the whole file is read.
*/

//Don't forget the import clauses:

import java.util.*;
import java.io.*;

//And here's the code for the METHOD. Sorry for the awful indentation:

      public double[][] readMatrixFile(String fileName)
      {
            double[][] output = null;
            try{
                  File f = new File(fileName);
                  byte[] chin = new byte[(int)f.length()];
                  FileInputStream fis = new FileInputStream(f);
                  fis.read(chin);
                  StringTokenizer st = new StringTokenizer(new String(chin));

                  int height = (new Integer(st.nextToken())).intValue();
                  int width = (new Integer(st.nextToken())).intValue();
                  output = new double[width][height];
                  for(int y=0;y<height;y++)
                  {
                        for(int x=0;x<width;x++)
                        {
                              output[x][y] = (new Double(st.nextToken())).doubleValue();
                        }
                  }
            }
            catch(Exception e)
            {
System.err.println(e);
                  System.err.println("Error processing matrix file "+fileName);
            }
            return output;
      }

//That's all folks!

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dufortCommented:
Comments:
-it should work well with space, newline and TAB separators
-If the file contains your matrix and some other stuff before and after, you will have to read it - and tokenize it - line by line.
-file handling in JDK1.1.x is very scattered and confusing. I guess the use of BufferedReader and ReadUTF() could enable you to read a file line by line and do the job right.

Have fun!

0
dufortCommented:
In the "catch()" part, you should add "output = null;" to help your program detect when there has been input errors.

I think that's all.

0
cokeman_Author Commented:
on a side note, would you be able to tell me how to concatenate a double to a string? ie, if I were to have a string str and I wanted to do something like
     str = str.concat(doubleNumber);
0
dufortCommented:
I believe this can be done with:

str = str.concat((new Double(doubleNumber)).toString());

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.