Solved

reading doubles from a file

Posted on 1998-05-02
9
168 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:cokeman_
9 Comments
 

Author Comment

by:cokeman_
ID: 1220431
actually, change that -- it doesn't have to be doubles that are read in -- integers are fine too
0
 

Expert Comment

by:beyondrob
ID: 1220432
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
 

Author Comment

by:cokeman_
ID: 1220433
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
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220434
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
dufort earned 50 total points
ID: 1220435
/*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
 
LVL 1

Expert Comment

by:dufort
ID: 1220436
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
 
LVL 1

Expert Comment

by:dufort
ID: 1220437
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
 

Author Comment

by:cokeman_
ID: 1220438
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
 
LVL 1

Expert Comment

by:dufort
ID: 1220439
I believe this can be done with:

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

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now