|
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
... ... ...
| |
Share this Question
| 10K+
P: 10,083
|
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. -
-
public static ArrayList readFile(String fileName) {
-
String line = "";
-
ArrayList data = new ArrayList();//consider using ArrayList<int>
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);//Can also use a Scanner to read the file
-
while((line = br.readLine()) != null) {
-
-
data.add(line);
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
return data;
-
}
-
For the second file you can make an ArrayList of ArrayList if you want to use the same method or an array of ArrayLists
| | |
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.
| | | 10K+
P: 10,083
|
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 -
-
public static ArrayList[] readFile2(String fileName) {
-
String line = "";
-
ArrayList[] data = new ArrayList[3];//consider using ArrayList<int>
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);//Can also use a Scanner to read the file
-
while((line = br.readLine()) != null) {
-
String[] theline = line.split(" ");
-
data[0].add(theline[0]);
-
data[1].add(theline[1]);
-
data[2].add(theline[2]);
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
return data;
-
}
-
| | |
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).
| | | 10K+
P: 10,083
|
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 -
-
import java.io.*;
-
import java.util.*;
-
public class ReadFile {
-
public static void main(String args[]) {
-
// You put your logic here
-
}
-
public static ArrayList[] readFile2(String fileName) {
-
String line = "";
-
ArrayList[] data = new ArrayList[3];
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);
-
while((line = br.readLine()) != null) {
-
String[] theline = line.split(" ");
-
data[0].add(Double.parseDouble(theline[0]));
-
data[1].add(Double.parseDouble(theline[1]));
-
data[2].add(Double.parseDouble(theline[2]));
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
return data;
-
}
-
public static ArrayList<Double> readFile(String fileName) {
-
String line = "";
-
ArrayList<Double> data = new ArrayList<Double>();
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);
-
while((line = br.readLine()) != null) {
-
data.add(Double.parseDouble(line));
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
return data;
-
}
-
}
-
| | |
P: 13
|
Please help to fix this code. | | | 10K+
P: 10,083
|
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
| | |
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.
| | | 10K+
P: 10,083
|
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?
| | |
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)
| | | 10K+
P: 10,083
|
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 -
-
public static ArrayList[] readFile2(String fileName) {
-
String line = "";
-
ArrayList[] data = new ArrayList[3];
-
for(int i = 0; i < 3; i++) {
-
data[i] = new ArrayList<Double>();
-
}
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);
-
while((line = br.readLine()) != null) {
-
String[] theline = line.split(" ");
-
data[0].add(Double.parseDouble(theline[0]));
-
data[1].add(Double.parseDouble(theline[1]));
-
data[2].add(Double.parseDouble(theline[2]));
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
return data;
-
}
-
and test it.
May I add that you could have realised that yourself.
| | |
P: 13
|
Thanks so much. It is running!
| | | 10K+
P: 10,083
|
Thanks so much. It is running!
Anytime Sky.
| | |
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. -
for (int i = 0; i < inWL[0].size(); i++) {
-
System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
-
fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
-
}
-
-
By the way, how could I modify my code to read a file with "tab delimited format"? -
public static ArrayList[] readFile2(String fileName) {
-
/ * File Structure:
-
* 1.1 11.1
-
* 2.2 22.2
-
* 3.3 33.3
-
* ... ...
-
*/
-
String line = " ";
-
ArrayList[] data = new ArrayList[2];
-
-
//for(int i = 0; i < 2; i++) {
-
data[0] = new ArrayList<String>();
-
data[1] = new ArrayList<Double>();
-
//}
-
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);
-
while((line = br.readLine()) != null) {
-
String[] theline = line.split(" ");
-
data[0].add(theline[0]);
-
data[1].add(Double.parseDouble(theline[1]));
-
-
//System.out.println(data[0] + " " + data[1]);
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
-
return data;
-
-
} //end readFile2 method
-
-
| | | 10K+
P: 10,083
|
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. -
for (int i = 0; i < inWL[0].size(); i++) {
-
System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
-
fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
-
}
-
-
By the way, how could I modify my code to read a file with "tab delimited format"? -
public static ArrayList[] readFile2(String fileName) {
-
/ * File Structure:
-
* 1.1 11.1
-
* 2.2 22.2
-
* 3.3 33.3
-
* ... ...
-
*/
-
String line = " ";
-
ArrayList[] data = new ArrayList[2];
-
-
//for(int i = 0; i < 2; i++) {
-
data[0] = new ArrayList<String>();
-
data[1] = new ArrayList<Double>();
-
//}
-
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);
-
while((line = br.readLine()) != null) {
-
String[] theline = line.split(" ");
-
data[0].add(theline[0]);
-
data[1].add(Double.parseDouble(theline[1]));
-
-
//System.out.println(data[0] + " " + data[1]);
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
-
return data;
-
-
} //end readFile2 method
-
-
The reading should be fine. It must be the line - String[] theline = line.split(" ");
Which is spilitting on space. You can split on any character
eg - 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.
| | |
P: 13
| -
FileWriter fout1 = new FileWriter("output1.txt");
-
for (int i = 0; i < inWL[0].size(); i++) {
-
System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
-
fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
-
}
-
-
| | | 10K+
P: 10,083
| -
FileWriter fout1 = new FileWriter("output1.txt");
-
for (int i = 0; i < inWL[0].size(); i++) {
-
System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
-
fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
-
}
-
-
I'd advise you the BufferedWriter class to write to a file -
-
BufferedWriter out = new BufferedWriter(new FileWriter(file));
-
out.write(s);//writes the String s
-
out.newLine();//goes to next line
-
| | |
P: 13
|
Thanks so much for your advice.
| | |
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. -
-
//This in main method
-
-
ArrayList[] inHAVCurve = new ArrayList[3];
-
String fin2 = "inHAVCurve.txt";
-
inHAVCurve = readFile3(fin2);
-
-
double[] a;
-
-
for (int i=0; i<inWL[0].size(); i++){
-
a[i] = inHAVCurve[1].get(i) + inHAVCurve[2].get(i);
-
}
-
-
public static ArrayList[] readFile3(String fileName) {
-
/* This method read file and store data in three ArrayLists[].
-
* File Structure:
-
* 1.1 11.1 111.1
-
* 2.2 22.2 222.2
-
* 3.3 33.3 333.3
-
* ... ... ...
-
*/
-
String line = "\t";
-
ArrayList[] data = new ArrayList[3];
-
-
for(int i = 0; i < 3; i++) {
-
data[i] = new ArrayList<Double>();
-
}
-
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);
-
while((line = br.readLine()) != null) {
-
String[] theline = line.split("\t");
-
data[0].add(Double.parseDouble(theline[0]));
-
data[1].add(Double.parseDouble(theline[1]));
-
data[2].add(Double.parseDouble(theline[2]));
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
-
return data;
-
-
} //end readFile3 method
-
-
-
| | | 10K+
P: 10,083
|
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. -
-
//This in main method
-
ArrayList[] inHAVCurve = new ArrayList[3];
-
String fin2 = "inHAVCurve.txt";
-
inHAVCurve = readFile3(fin2);
-
-
double[] a;
-
-
for (int i=0; i<inWL[0].size(); i++){
-
a[i] = inHAVCurve[1].get(i) + inHAVCurve[2].get(i);
-
}
-
-
public static ArrayList[] readFile3(String fileName) {
-
/* This method read file and store data in three ArrayLists[].
-
* File Structure:
-
* 1.1 11.1 111.1
-
* 2.2 22.2 222.2
-
* 3.3 33.3 333.3
-
* ... ... ...
-
*/
-
String line = "\t";
-
ArrayList[] data = new ArrayList[3];
-
-
for(int i = 0; i < 3; i++) {
-
data[i] = new ArrayList<Double>();
-
}
-
-
try {
-
FileReader fr = new FileReader(fileName);
-
BufferedReader br = new BufferedReader(fr);
-
while((line = br.readLine()) != null) {
-
String[] theline = line.split("\t");
-
data[0].add(Double.parseDouble(theline[0]));
-
data[1].add(Double.parseDouble(theline[1]));
-
data[2].add(Double.parseDouble(theline[2]));
-
}
-
}
-
catch(FileNotFoundException fN) {
-
fN.printStackTrace();
-
}
-
-
catch(IOException e) {
-
System.out.println(e);
-
}
-
-
return data;
-
-
} //end readFile3 method
-
-
-
- ArrayList[] inHAVCurve = readFile3(fin2);
-
-
double[] a;
-
-
for(int j = 0; j<inHAVCurve[1].size();j++) {
-
a[i] = inHAVCurve[1].get(j) + inHAVCurve[2].get(j);
-
}
-
For this to work, inHAVCurve[1] and inHAVCurve[2] must have the same number of elements.
| | |
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
| | | 10K+
P: 10,083
|
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 -
-
a[i] = (Double)inHAVCurve[1].get(j) + (Double)inHAVCurve[2].get(j);
-
| | |
P: 13
|
It still cannot run. Index of a[j] is j, not i.
| | | 10K+
P: 10,083
|
It still cannot run. Index of a[j] is j, not i.
-
-
ArrayList[] inHAVCurve = readFile3(fin2);
-
double[] a = new double[inHAVCurve[0].size()];
-
for(int j = 0; j<inHAVCurve[1].size();j++) {
-
a[j] = (Double)inHAVCurve[1].get(j) + (Double)inHAVCurve[2].get(j);
-
}
-
You should also put an effort of trying to correct some of the errors yourself.
| | Post your reply Help answer this question
Didn't find the answer to your Java question?
You can also browse similar questions: Java | | Question stats - viewed: 47899
- replies: 24
- date asked: Jan 9 '07
|