By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

Reading data into array from file

P: 13
Hello, I am a new Java learner. I would like to develop one model by using Java. In that model, there are many files to read. I want to write a method in Java to read a file and store the data in array(s). Later, I just only call that method to read other files. Would you please help me to write a code to read the data in the following formats:

First format (for example, separated by lines): store one column of the data in one array

11.1
22.2
33.3
...

Second format (for example, separated by spaces): store one column of the data in one array, so there are three arrays

1.1 11.1 111.1
2.2 22.2 222.2
3.3 33.3 333.3
... ... ...
Jan 9 '07 #1
Share this Question
Share on Google+
24 Replies


10K+
P: 13,264
Hello, I am a new Java learner. I would like to develop one model by using Java. In that model, there are many files to read. I want to write a method in Java to read a file and store the data in array(s). Later, I just only call that method to read other files. Would you please help me to write a code to read the data in the following formats:

First format (for example, separated by lines): store one column of the data in one array

11.1
22.2
33.3
...

Second format (for example, separated by spaces): store one column of the data in one array, so there are three arrays

1.1 11.1 111.1
2.2 22.2 222.2
3.3 33.3 333.3
... ... ...
Do you want to use one method to read both types of files?
Store the values in ArrayList rather than an Array so you don't have to find the number of lines in the files first.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static ArrayList readFile(String fileName) {
  3.   String line = "";
  4.   ArrayList data = new ArrayList();//consider using ArrayList<int>
  5.   try {
  6.    FileReader fr = new FileReader(fileName);
  7.    BufferedReader br = new BufferedReader(fr);//Can also use a Scanner to read the file
  8.    while((line = br.readLine()) != null) {
  9.  
  10.     data.add(line);
  11.    }
  12.   }
  13.   catch(FileNotFoundException fN) {
  14.    fN.printStackTrace();
  15.   }
  16.   catch(IOException e) {
  17.    System.out.println(e);
  18.   }
  19.   return data;
  20.  }   
  21.  
For the second file you can make an ArrayList of ArrayList if you want to use the same method or an array of ArrayLists
Jan 9 '07 #2

P: 13
Thanks for your help and comments. If possible, I would like to use two different methods to read the two different formats of the data files.
Jan 9 '07 #3

10K+
P: 13,264
Thanks for your help and comments. If possible, I would like to use two different methods to read the two different formats of the data files.
You could write the second one along the lines of

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static ArrayList[] readFile2(String fileName) {
  3.   String line = "";
  4.   ArrayList[] data = new ArrayList[3];//consider using ArrayList<int>
  5.   try {
  6.    FileReader fr = new FileReader(fileName);
  7.    BufferedReader br = new BufferedReader(fr);//Can also use a Scanner to read the file
  8.    while((line = br.readLine()) != null) {
  9.     String[] theline = line.split(" ");
  10.     data[0].add(theline[0]);
  11.     data[1].add(theline[1]);
  12.     data[2].add(theline[2]);
  13.    }
  14.   }
  15.   catch(FileNotFoundException fN) {
  16.    fN.printStackTrace();
  17.   }
  18.   catch(IOException e) {
  19.    System.out.println(e);
  20.   }
  21.   return data;
  22.  }
  23.  
Jan 9 '07 #4

P: 13
Could you make it in a complete class that is ready to run? For example, there is two files for the first format (so there are two arrays) and one file for second format (so there are three arrays). I would like to get those arrays in double type and it is ready to use for computation. Then I would like to write the two arrays from the first format to one file with two columns (first column for first array and second column for second array) . The arrays from the second format are written in two files. First file is for the first array and second file is for the three arrays (I would like to reproduce the second format file).
Jan 9 '07 #5

10K+
P: 13,264
Could you make it in a complete class that is ready to run? For example, there is two files for the first format (so there are two arrays) and one file for second format (so there are three arrays). I would like to get those arrays in double type and it is ready to use for computation. Then I would like to write the two arrays from the first format to one file with two columns (first column for first array and second column for second array) . The arrays from the second format are written in two files. First file is for the first array and second file is for the three arrays (I would like to reproduce the second format file).
Sorry I had gone out for a while.

Have a go at it first and post some code then I can see if you need help

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. public class ReadFile {
  5.  public static void main(String args[]) {
  6.   // You put your logic here
  7.  }
  8.  public static ArrayList[] readFile2(String fileName) {
  9.   String line = "";
  10.   ArrayList[] data = new ArrayList[3];
  11.   try {
  12.    FileReader fr = new FileReader(fileName);
  13.    BufferedReader br = new BufferedReader(fr);
  14.    while((line = br.readLine()) != null) {
  15.     String[] theline = line.split(" ");
  16.     data[0].add(Double.parseDouble(theline[0]));
  17.     data[1].add(Double.parseDouble(theline[1]));
  18.     data[2].add(Double.parseDouble(theline[2]));
  19.    }
  20.   }
  21.   catch(FileNotFoundException fN) {
  22.    fN.printStackTrace();
  23.   }
  24.   catch(IOException e) {
  25.    System.out.println(e);
  26.   }
  27.   return data;
  28.  }
  29.  public static ArrayList<Double> readFile(String fileName) {
  30.   String line = "";
  31.   ArrayList<Double> data = new ArrayList<Double>();
  32.   try {
  33.    FileReader fr = new FileReader(fileName);
  34.    BufferedReader br = new BufferedReader(fr);
  35.    while((line = br.readLine()) != null) {
  36.     data.add(Double.parseDouble(line));
  37.    }
  38.   }
  39.   catch(FileNotFoundException fN) {
  40.    fN.printStackTrace();
  41.   }
  42.   catch(IOException e) {
  43.    System.out.println(e);
  44.   }
  45.   return data;
  46.  }
  47. }
  48.  
Jan 9 '07 #6

P: 13
Please help to fix this code.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class ReadingFile {
  6.  
  7. public static void main(String args[]) {
  8.     // You put your logic here
  9.     int i,j;
  10.     int count1,count2;        // number of the array elements
  11.  
  12.     int maxSize1 = 100;        // max number of array elements
  13.     double[] dataArray1 = new double[maxSize1];
  14.     double[] dataArray2 = new double[maxSize1];
  15.  
  16.     int maxSize2 = 50;
  17.     double[] dataArray3 = new double[maxSize2];
  18.     double[] dataArray4 = new double[maxSize2];
  19.     double[] dataArray5 = new double[maxSize2];
  20.  
  21.     String fn1 = "file1.txt";
  22.     String fn2 = "file2.txt";
  23.     String fn3 = "file3.txt";
  24.     /* first format: file1.txt and file2.txt
  25.        11.1
  26.        22.2
  27.        33.3
  28.        ...
  29.  
  30.        second format: file3.txt
  31.        1.1    11.1    111.1
  32.        2.2    22.2    222.2
  33.        3.3    33.3    333.3
  34.        ...    ...        ...
  35.  
  36.      */
  37.  
  38.  
  39.     dataArray1 = readFile(fn1);    // I want to store the data from the first file
  40.     dataArray2 = readFile(fn2);    // I want to store the data from the second file
  41.  
  42.     dataArray3 = readFile(fn3);    // I want to store the data of first colunm of the third file
  43.     dataArray4 = readFile(fn3);    // I want to store the data of second colunm of the third file
  44.     dataArray5 = readFile(fn3);    // I want to store the data of third colunm of the third file
  45.  
  46.     // I want to write the data that I read to a file
  47.     FileWriter fout1 = new FileWriter("output1.txt");
  48.     FileWriter fout2 = new FileWriter("output2.txt");
  49.  
  50.     for (i=0; (i<count1); i++){
  51.         fout1.write(dataArray1[i]+" "+dataArray2[i]);    // I want to write to a file with two colonms
  52.  
  53.     }
  54.     fout1.close();
  55.  
  56.     for (j=0; (j<count2); j++){
  57.         fout2.write(dataArray3[j]+" "+dataArray4[j]+" "+dataArray4[j]);    // I want to write to reproduce the third file
  58.  
  59.     }
  60.     fout2.close();
  61.  
  62. }
  63. // I want to get count2(number of elements in each array) 
  64. public static ArrayList[] readFile2(String fileName) {
  65.   String line = "";
  66.   ArrayList[] data = new ArrayList[3];
  67.   try {
  68.    FileReader fr = new FileReader(fileName);
  69.    BufferedReader br = new BufferedReader(fr);
  70.    while((line = br.readLine()) != null) {
  71.     String[] theline = line.split(" ");
  72.     data[0].add(Double.parseDouble(theline[0]));
  73.     data[1].add(Double.parseDouble(theline[1]));
  74.     data[2].add(Double.parseDouble(theline[2]));
  75.    }
  76.   }
  77.   catch(FileNotFoundException fN) {
  78.    fN.printStackTrace();
  79.   }
  80.   catch(IOException e) {
  81.    System.out.println(e);
  82.   }
  83.   return data;
  84.  }
  85. //I want to get count1(number of elements of the array) 
  86.  public static ArrayList<Double> readFile(String fileName) {
  87.   String line = "";
  88.   ArrayList<Double> data = new ArrayList<Double>();
  89.   try {
  90.    FileReader fr = new FileReader(fileName);
  91.    BufferedReader br = new BufferedReader(fr);
  92.    while((line = br.readLine()) != null) {
  93.     data.add(Double.parseDouble(line));
  94.    }
  95.   }
  96.   catch(FileNotFoundException fN) {
  97.    fN.printStackTrace();
  98.   }
  99.   catch(IOException e) {
  100.    System.out.println(e);
  101.   }
  102.   return data;
  103.  }
  104. }
  105.  
  106.  
Jan 9 '07 #7

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3. import java.util.*; 
  4. public class ReadingFile1 {
  5.  
  6. public static void main(String args[]) {
  7.  // You put your logic here
  8.  
  9.  //Simply use ArrayList.size()
  10.  //int i,j;
  11.  //int count1,count2;  // number of the array elements
  12.  
  13.  //int maxSize1 = 100;  not neccessary
  14.  
  15.  //The methods are returning ArrayLists not arrays
  16.  ArrayList<Double> dataArray1 = new ArrayList<Double>();
  17.  ArrayList<Double> dataArray2 = new ArrayList<Double>();
  18.  
  19.  //only one array is required here
  20.  ArrayList[] dataArray3 = new ArrayList[3];
  21.  
  22.  
  23.  //int maxSize2 = 50;
  24.  //double[] dataArray3 = new double[maxSize2];
  25.  //double[] dataArray4 = new double[maxSize2];
  26.  //double[] dataArray5 = new double[maxSize2];
  27.  
  28.  String fn1 = "file1.txt";
  29.  String fn2 = "file2.txt";
  30.  String fn3 = "file3.txt";
  31.  /* first format: file1.txt and file2.txt
  32.     11.1
  33.     22.2
  34.     33.3
  35.     ...
  36.  
  37.     second format: file3.txt
  38.     1.1 11.1 111.1
  39.     2.2 22.2 222.2
  40.     3.3 33.3 333.3
  41.     ... ...  ...
  42.  
  43.   */
  44.  
  45.  
  46.  dataArray1 = readFile(fn1); // I want to store the data from the first file
  47.  dataArray2 = readFile(fn2); // I want to store the data from the second file
  48.  // call the other method here
  49.  dataArray3 = readFile2(fn3);
  50.  
  51.  
  52.  //dataArray3 = readFile(fn3); // I want to store the data of first colunm of the third file
  53.  //dataArray4 = readFile(fn3); // I want to store the data of second colunm of the third file
  54.  //dataArray5 = readFile(fn3); // I want to store the data of third colunm of the third file
  55.  
  56.  // I want to write the data that I read to a file
  57.  //You must handle the possible IOEeceptions
  58.  try {
  59.   FileWriter fout1 = new FileWriter("output1.txt");
  60.   FileWriter fout2 = new FileWriter("output2.txt");
  61.   for (int i = 0; i < dataArray1.size(); i++){
  62.    fout1.write(dataArray1.get(i) + " " + dataArray2.get(i)); // I want to write to a file with two colonms
  63.   }
  64.   fout1.close();
  65.  }
  66.  catch(IOException iO) {
  67.   iO.printStackTrace();
  68.  }
  69.  
  70.  //Make sure the first one is working first
  71.  //for (j=0; (j<count2); j++){
  72.  // fout2.write(dataArray3[j]+" "+dataArray4[j]+" "+dataArray4[j]); // I want to write to reproduce the third file
  73.  
  74.  //}
  75.  //fout2.close();
  76.  
  77. }
  78. // I want to get count2(number of elements in each array)
  79. public static ArrayList[] readFile2(String fileName) {
  80.   String line = "";
  81.   ArrayList[] data = new ArrayList[3];
  82.   try {
  83.    FileReader fr = new FileReader(fileName);
  84.    BufferedReader br = new BufferedReader(fr);
  85.    while((line = br.readLine()) != null) {
  86.  String[] theline = line.split(" ");
  87.  data[0].add(Double.parseDouble(theline[0]));
  88.  data[1].add(Double.parseDouble(theline[1]));
  89.  data[2].add(Double.parseDouble(theline[2]));
  90.    }
  91.   }
  92.   catch(FileNotFoundException fN) {
  93.    fN.printStackTrace();
  94.   }
  95.   catch(IOException e) {
  96.    System.out.println(e);
  97.   }
  98.   return data;
  99.  }
  100. //I want to get count1(number of elements of the array)
  101.  public static ArrayList<Double> readFile(String fileName) {
  102.   String line = "";
  103.   ArrayList<Double> data = new ArrayList<Double>();
  104.   try {
  105.    FileReader fr = new FileReader(fileName);
  106.    BufferedReader br = new BufferedReader(fr);
  107.    while((line = br.readLine()) != null) {
  108.  data.add(Double.parseDouble(line));
  109.    }
  110.   }
  111.   catch(FileNotFoundException fN) {
  112.    fN.printStackTrace();
  113.   }
  114.   catch(IOException e) {
  115.    System.out.println(e);
  116.   }
  117.   return data;
  118.  }
  119. }
  120.  
  121.  
  122.  
Have a look at the corrections and try and see if it works.
If there's any line you don't ubderstand there you might as well ask
Jan 9 '07 #8

P: 13
Thanks so much for your help. I can successfully run the first method (readFile). But I still cannot run the second method (readFile2). It seems there are problems in declaration of dataArray3, calling method readFile2 and in the method itself.
Jan 10 '07 #9

10K+
P: 13,264
Thanks so much for your help. I can successfully run the first method (readFile). But I still cannot run the second method (readFile2). It seems there are problems in declaration of dataArray3, calling method readFile2 and in the method itself.
You'll have to be more specific. What problems are these compile time errors?
Jan 10 '07 #10

P: 13
It shows this in console:
Exception in thread "main" java.lang.NullPointerException
at fileReading.readFile2(fileReading.java:95)
at fileReading.main(fileReading.java:49)
Jan 10 '07 #11

10K+
P: 13,264
It shows this in console:
Exception in thread "main" java.lang.NullPointerException
at fileReading.readFile2(fileReading.java:95)
at fileReading.main(fileReading.java:49)
Yep I had not initialised the array elements. Change the method to


Expand|Select|Wrap|Line Numbers
  1.  
  2. public static ArrayList[] readFile2(String fileName) {
  3.   String line = "";
  4.   ArrayList[] data = new ArrayList[3];
  5.   for(int i = 0; i < 3; i++) {
  6.    data[i] = new ArrayList<Double>();
  7.   }
  8.   try {
  9.    FileReader fr = new FileReader(fileName);
  10.    BufferedReader br = new BufferedReader(fr);
  11.    while((line = br.readLine()) != null) {
  12.  String[] theline = line.split(" ");
  13.  data[0].add(Double.parseDouble(theline[0]));
  14.  data[1].add(Double.parseDouble(theline[1]));
  15.  data[2].add(Double.parseDouble(theline[2]));
  16.    }
  17.   }
  18.   catch(FileNotFoundException fN) {
  19.    fN.printStackTrace();
  20.   }
  21.   catch(IOException e) {
  22.    System.out.println(e);
  23.   }
  24.   return data;
  25.  }
  26.  
and test it.

May I add that you could have realised that yourself.
Jan 10 '07 #12

P: 13
Thanks so much. It is running!
Jan 10 '07 #13

10K+
P: 13,264
Thanks so much. It is running!
Anytime Sky.
Jan 10 '07 #14

P: 13
I can run the class. But when I use "space delimited format" for an input file, I could not write all read data to a file. The read data are displayed normally when I write it to the console.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < inWL[0].size(); i++) {
  2.     System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
  3.     fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
  4. }
  5.  
  6.  
By the way, how could I modify my code to read a file with "tab delimited format"?

Expand|Select|Wrap|Line Numbers
  1. public static ArrayList[] readFile2(String fileName) {
  2.            / * File Structure:
  3.          *     1.1 11.1
  4.          *     2.2 22.2
  5.          *  3.3 33.3
  6.          *     ... ... 
  7.          */    
  8.             String line = " ";
  9.             ArrayList[] data = new ArrayList[2];
  10.  
  11.             //for(int i = 0; i < 2; i++) {
  12.             data[0] = new ArrayList<String>();
  13.             data[1] = new ArrayList<Double>();
  14.             //}
  15.  
  16.             try {
  17.                 FileReader fr = new FileReader(fileName);
  18.                 BufferedReader br = new BufferedReader(fr);
  19.                 while((line = br.readLine()) != null) {
  20.                     String[] theline = line.split(" ");
  21.                     data[0].add(theline[0]);
  22.                     data[1].add(Double.parseDouble(theline[1]));
  23.  
  24.                     //System.out.println(data[0] + "   " + data[1]);
  25.                 }
  26.             }
  27.             catch(FileNotFoundException fN) {
  28.                 fN.printStackTrace();
  29.             }
  30.  
  31.             catch(IOException e) {
  32.                 System.out.println(e);
  33.             }
  34.  
  35.             return data;
  36.  
  37.         } //end readFile2 method
  38.  
  39.  
Jan 11 '07 #15

10K+
P: 13,264
I can run the class. But when I use "space delimited format" for an input file, I could not write all read data to a file. The read data are displayed normally when I write it to the console.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < inWL[0].size(); i++) {
  2.     System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
  3.     fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
  4. }
  5.  
  6.  
By the way, how could I modify my code to read a file with "tab delimited format"?

Expand|Select|Wrap|Line Numbers
  1. public static ArrayList[] readFile2(String fileName) {
  2.      / * File Structure:
  3.          *     1.1 11.1
  4.          *     2.2 22.2
  5.          * 3.3 33.3
  6.          *     ... ... 
  7.          */    
  8.             String line = " ";
  9.             ArrayList[] data = new ArrayList[2];
  10.  
  11.             //for(int i = 0; i < 2; i++) {
  12.             data[0] = new ArrayList<String>();
  13.             data[1] = new ArrayList<Double>();
  14.             //}
  15.  
  16.             try {
  17.                 FileReader fr = new FileReader(fileName);
  18.                 BufferedReader br = new BufferedReader(fr);
  19.                 while((line = br.readLine()) != null) {
  20.                     String[] theline = line.split(" ");
  21.                     data[0].add(theline[0]);
  22.                     data[1].add(Double.parseDouble(theline[1]));
  23.  
  24.                     //System.out.println(data[0] + " " + data[1]);
  25.                 }
  26.             }
  27.             catch(FileNotFoundException fN) {
  28.                 fN.printStackTrace();
  29.             }
  30.  
  31.             catch(IOException e) {
  32.                 System.out.println(e);
  33.             }
  34.  
  35.             return data;
  36.  
  37.         } //end readFile2 method
  38.  
  39.  
The reading should be fine. It must be the line
Expand|Select|Wrap|Line Numbers
  1. String[] theline = line.split(" ");
Which is spilitting on space. You can split on any character

eg
Expand|Select|Wrap|Line Numbers
  1.  String[] theLine = line.split("\t");
to split on tab.

You said you could not print all the data to the other file but could print all the data to the console? If so then post the code you used for printing to the file.
Jan 11 '07 #16

P: 13
Expand|Select|Wrap|Line Numbers
  1. FileWriter fout1 = new FileWriter("output1.txt");
  2. for (int i = 0; i < inWL[0].size(); i++) {
  3.     System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
  4.     fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
  5. }
  6.  
  7.  
Jan 11 '07 #17

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1. FileWriter fout1 = new FileWriter("output1.txt");
  2. for (int i = 0; i < inWL[0].size(); i++) {
  3.     System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
  4.     fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
  5. }
  6.  
  7.  
I'd advise you the BufferedWriter class to write to a file

Expand|Select|Wrap|Line Numbers
  1.  
  2. BufferedWriter out = new BufferedWriter(new FileWriter(file));
  3. out.write(s);//writes the String s
  4. out.newLine();//goes to next line
  5.  
Jan 11 '07 #18

P: 13
Thanks so much for your advice.
Jan 11 '07 #19

P: 13
How could I do the operation on the ArrayList? Could I transfer all the element values in ArrayList[1] to an array?
The following is my problem. I could not do the calculation in the main method.
Expand|Select|Wrap|Line Numbers
  1.  
  2. //This in main method
  3.  
  4. ArrayList[] inHAVCurve = new ArrayList[3];
  5. String fin2 = "inHAVCurve.txt";
  6. inHAVCurve = readFile3(fin2);
  7.  
  8. double[] a;
  9.  
  10. for (int i=0; i<inWL[0].size(); i++){
  11.     a[i] = inHAVCurve[1].get(i) + inHAVCurve[2].get(i);
  12. }
  13.  
  14. public static ArrayList[] readFile3(String fileName) {
  15.     /* This method read file and store data in three ArrayLists[].   
  16.      * File Structure:
  17.      *     1.1 11.1 111.1
  18.      *     2.2 22.2 222.2
  19.      *     3.3 33.3 333.3
  20.      *     ... ...  ...
  21.      */    
  22.         String line = "\t";
  23.         ArrayList[] data = new ArrayList[3];
  24.  
  25.         for(int i = 0; i < 3; i++) {
  26.         data[i] = new ArrayList<Double>();
  27.         }
  28.  
  29.         try {
  30.             FileReader fr = new FileReader(fileName);
  31.             BufferedReader br = new BufferedReader(fr);
  32.             while((line = br.readLine()) != null) {
  33.                 String[] theline = line.split("\t");
  34.                 data[0].add(Double.parseDouble(theline[0]));
  35.                 data[1].add(Double.parseDouble(theline[1]));
  36.                 data[2].add(Double.parseDouble(theline[2]));
  37.             }
  38.         }
  39.         catch(FileNotFoundException fN) {
  40.             fN.printStackTrace();
  41.         }
  42.  
  43.         catch(IOException e) {
  44.             System.out.println(e);
  45.         }
  46.  
  47.         return data;
  48.  
  49.     } //end readFile3 method
  50.  
  51.  
  52.  
Jan 12 '07 #20

10K+
P: 13,264
How could I do the operation on the ArrayList? Could I transfer all the element values in ArrayList[1] to an array?
The following is my problem. I could not do the calculation in the main method.
Expand|Select|Wrap|Line Numbers
  1.  
  2. //This in main method
  3.  ArrayList[] inHAVCurve = new ArrayList[3];
  4. String fin2 = "inHAVCurve.txt";
  5. inHAVCurve = readFile3(fin2);
  6.  
  7. double[] a;
  8.  
  9. for (int i=0; i<inWL[0].size(); i++){
  10.     a[i] = inHAVCurve[1].get(i) + inHAVCurve[2].get(i);
  11. }
  12.  
  13. public static ArrayList[] readFile3(String fileName) {
  14.     /* This method read file and store data in three ArrayLists[]. 
  15.      * File Structure:
  16.      *     1.1 11.1 111.1
  17.      *     2.2 22.2 222.2
  18.      *     3.3 33.3 333.3
  19.      *     ... ... ...
  20.      */    
  21.         String line = "\t";
  22.         ArrayList[] data = new ArrayList[3];
  23.  
  24.         for(int i = 0; i < 3; i++) {
  25.         data[i] = new ArrayList<Double>();
  26.         }
  27.  
  28.         try {
  29.             FileReader fr = new FileReader(fileName);
  30.             BufferedReader br = new BufferedReader(fr);
  31.             while((line = br.readLine()) != null) {
  32.                 String[] theline = line.split("\t");
  33.                 data[0].add(Double.parseDouble(theline[0]));
  34.                 data[1].add(Double.parseDouble(theline[1]));
  35.                 data[2].add(Double.parseDouble(theline[2]));
  36.             }
  37.         }
  38.         catch(FileNotFoundException fN) {
  39.             fN.printStackTrace();
  40.         }
  41.  
  42.         catch(IOException e) {
  43.             System.out.println(e);
  44.         }
  45.  
  46.         return data;
  47.  
  48.     } //end readFile3 method
  49.  
  50.  
  51.  
Expand|Select|Wrap|Line Numbers
  1.  ArrayList[] inHAVCurve  = readFile3(fin2); 
  2.  
  3. double[] a;
  4.  
  5.   for(int j = 0; j<inHAVCurve[1].size();j++) {
  6.     a[i] = inHAVCurve[1].get(j) + inHAVCurve[2].get(j);
  7.  }
  8.  
For this to work, inHAVCurve[1] and inHAVCurve[2] must have the same number of elements.
Jan 12 '07 #21

P: 13
Yes, they have same number of elements. I tried it, but it still does not work. I got this in the console:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator + is undefined for the argument type(s) java.lang.Object, java.lang.Object
Jan 12 '07 #22

10K+
P: 13,264
Yes, they have same number of elements. I tried it, but it still does not work. I got this in the console:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator + is undefined for the argument type(s) java.lang.Object, java.lang.Object
You have to type cast the elements from the ArrayList to doubles like
Expand|Select|Wrap|Line Numbers
  1.  
  2. a[i] = (Double)inHAVCurve[1].get(j) + (Double)inHAVCurve[2].get(j);
  3.  
Jan 12 '07 #23

P: 13
It still cannot run. Index of a[j] is j, not i.
Jan 12 '07 #24

10K+
P: 13,264
It still cannot run. Index of a[j] is j, not i.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ArrayList[] inHAVCurve  = readFile3(fin2); 
  3. double[] a = new double[inHAVCurve[0].size()];
  4. for(int j = 0; j<inHAVCurve[1].size();j++) {
  5.  a[j] = (Double)inHAVCurve[1].get(j) + (Double)inHAVCurve[2].get(j);
  6. }
  7.  
You should also put an effort of trying to correct some of the errors yourself.
Jan 12 '07 #25

Post your reply

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