473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Java question: How do I read a file into an double array; and then write it out to s

1
Hi, I am doing this homework where I am suppose to read a .txt file fill with int number into a double array. After which I had to write this double array into another .txt file.
At first I though everything was working as I was able to read the file and display it out as an image, and then save it into a .txt file as double. However when I tried to use the same class file for another lesson (display it in frame I created), I keep getting 0.0 as the value of the output .txt file. Below is the code for my read and write class:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class IO {
  7.     FileDialog fd;
  8.     public double[][] readData() { 
  9.         fd = new FileDialog(new Frame(), "Open Files", FileDialog.LOAD);
  10.         fd.setVisible(true);
  11.         File f = null;
  12.         if ((fd.getDirectory() != null)||( fd.getFile() != null)) {
  13.             f = new File(fd.getDirectory() + fd.getFile());
  14.         }
  15.         FileReader fr = null;
  16.         try {
  17.             fr = new FileReader (f);
  18.         } catch (FileNotFoundException fnfe) {
  19.             fnfe.printStackTrace();
  20.         }
  21.         BufferedReader br = new BufferedReader(fr);
  22.         int lines = -1;
  23.         String textIn = " ";
  24.         String[] file = null;
  25.         try {
  26.             while (textIn != null) {
  27.                 textIn = br.readLine();
  28.                 lines++;
  29.             }
  30.             file = new String[lines];
  31.             fr = new FileReader (f);
  32.             br = new BufferedReader(fr);
  33.             for (int i = 0; i < lines; i++) {
  34.                 file[i] = br.readLine();
  35.             }
  36.             br.close();
  37.         } catch (IOException ioe) {
  38.             ioe.printStackTrace();
  39.         }
  40.         double[][] data = new double [lines][];
  41.         for (int i = 0; i < lines; i++) {
  42.             StringTokenizer st = new StringTokenizer(file[i],",");
  43.             data[i] = new double[st.countTokens()];
  44.             int j = 0;
  45.             while (st.hasMoreTokens()) {
  46.                 data[i][j] = Double.parseDouble(st.nextToken());
  47.                 j++;
  48.             }
  49.         }
  50.         return data;
  51.     }
  52.  
  53.  
  54.     public void writeData(double[][] dataIn) {
  55.         fd = new FileDialog(new Frame(), "Save Files", FileDialog.SAVE);
  56.         fd.setVisible(true);
  57.         File f = null;
  58.         if ((fd.getDirectory() != null)||( fd.getFile() != null)) {
  59.             f = new File(fd.getDirectory() + fd.getFile());
  60.         }
  61.         FileWriter fw = null;
  62.         try {
  63.             fw = new FileWriter (f, true);
  64.         } catch (IOException ioe) { 
  65.             ioe.printStackTrace();
  66.         }
  67.         BufferedWriter bw = new BufferedWriter (fw);
  68.         String tempStr = "";
  69.         try {
  70.             for (int i = 0; i < dataIn.length; i++) {
  71.                 for (int j = 0; j < dataIn[i].length; j++) {
  72.                     tempStr = String.valueOf(dataIn[i][j]);
  73.                     bw.write(tempStr);
  74.                 }
  75.                 bw.newLine();
  76.             }
  77.             bw.close();
  78.         } catch (IOException ioe) {
  79.             ioe.printStackTrace();
  80.         }
  81.     }
  82. }
  83.  
Any help will be greatly appreciated.
Jul 1 '12 #1
0 1332

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
2
by: antonio matos | last post by:
hi people. I have to acess a database, and there are some fields that are binary. when i acess that field is read like an array of bytes. but in reality it's an array of doubles!!! how can...
0
by: suryar | last post by:
Hi please help me some one in my company i want to write a script in vb to write a text file but the source file is an excel file i want write a text or dat file with fixed lengths maping with ...
3
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
5
by: Kiran | last post by:
Hi all, another newbie question from me, but here goes. Ok, I have a double array defined as follows: int cluster; now, i know that if you say for example cluster, this will give me the...
2
by: mathGirl | last post by:
Hi, I am not computer savvy at all. i am a math major so I am utterly lost I need to read information form a text file i created called Item this is the file where
4
by: npm | last post by:
I'm setting up a polling system where voters can only vote once, based on their IP address. When the poll loads I want to open the text file with the list of blocked IPs to see if a voter has...
4
by: Pam Golembiewsk | last post by:
I declared a double called annualIntRt earlier in my program, so other modules could access the data. The file is being read as double values, all I have to do is fill my array with them, but I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.