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
... ... ...
24 83543
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
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.
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;
-
}
-
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).
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;
-
}
-
}
-
Please help to fix this code.
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
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.
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?
It shows this in console:
Exception in thread "main" java.lang.NullPointerException
at fileReading.readFile2(fileReading.java:95)
at fileReading.main(fileReading.java:49)
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.
Thanks so much. It is running!
Thanks so much. It is running!
Anytime Sky.
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
-
-
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.
-
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");
-
}
-
-
-
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
-
Thanks so much for your advice.
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
-
-
-
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.
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
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);
-
It still cannot run. Index of a[j] is j, not i.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: matt |
last post by:
I'm reading lines from a file to display an image. Each image and
caption is on a new line. I read the string from each line, up until the
line break (\r\n) and then assume my fields are starting...
|
by: Satish Chimakurthi |
last post by:
Hi all,
I have data of the following form in a file called "fluid_grid.dat"
1.00000000000000 0.00000000000000D+000
0.959753969508636 0.280842158538236
0.842255363975169 ...
|
by: SB |
last post by:
Hello. I have an input file which is laid out in the following manner...
Name
Day 1
am time 1
am time 2
appointment
pm time 1
pm time 2
appointment
Day 2
|
by: melanieab |
last post by:
Hi,
I'm trying to store all of my data into one file (there're about 140 things
to keep track of). I have no problem reading a specific string from the
array file, but I wasn't sure how to...
|
by: tlcvetan158 |
last post by:
Hi, I'm a newbie at C++. I am just trying to read a file with three columns of data into arrays and write it back out, to see how arrays work.
This runs with no errors, but the output doesn't look...
|
by: becca0619 |
last post by:
Hey, Ive been looking around to find out how to read data into a 2D array and I cant find a solution that will fit my assignment. Im not sure that I should even use an array actually...
ok so...
|
by: Naeem |
last post by:
When i try to read a file using fstream in borland C++, result is
displayed with unusual characters.
I read a file "named ffile.txt" using ffile.read() or ffile>into
variables of same width as...
|
by: pp08 |
last post by:
Hi All,
I am a fresher to C++.. I got an assignment but while doing that I am not able to find the way for below issue.. Please have a look and let me know how can I start..
I wanted to read a...
|
by: triphoppa |
last post by:
Ok, I'm trying to read in some integers from a file for some reason however my array is not filling up. I know it's not filling up because I can watch the array in the debugger. My code looks...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |