Results 1 to 20 of 37
- 07-17-2008, 11:37 PM #1
Reading two text file and sum them up
Hi, going to learn some Java for fun,
I just wanna know how to make a program that can read two text files("InputFile1.txt" and "InputFile2.txt"), sum up the value line by line from both files and saved at the output file name "sum.txt"
InputFile1.txt
InputFile2.txt1.2
0.1
2.5
4.6
9.1
10.3
12.6
2.2
7.1
4.51
4.6
19.1
10.3
0.0
1.22
-2.1
9.5
12
43
77.1
0.5
I have simply created two text files InputFile1.txt have 21 values and InputFile2.txt have 20 values.1.3
9.1
2.9
0.6
9.1
10.8
17.6
22.2
7.11
3.51
0.6
9.1
0.3
0.01
1.22
-2.1
9.51
2
4.3
7.1
Add them up line by line from both file and just assume the 21st value at InputFile2.txt is zero.
The ouput at sum.txt:
Any guides from expert would be appreciate as I never seen a Java program that can read two txt files.2.5
9.2
5.4
5.2
18.2
21.1
30.2
24.4
14.21
8.02
5.2
28.2
10.6
0.01
2.44
-4.2
19.01
14
47.3
84.2
0.5
Thanks.
- 07-18-2008, 12:44 AM #2
I guess you'd write a program that would read lines from the two input files, convert the data being read on each line into a double, add the two numbers together and write the output to the third file.
So research how to read a file and how to write a file. There are lots of examples of code you could search for. Look for BufferedReader and BufferedWriter for example. Study those programs and see if you can then write your own.
Good luck.
- 07-18-2008, 08:51 AM #3
Yes Norm is absolutely rite.
You can read the values from file and store them in "array" of type double perform the addition and then write it back to another txt file.
The basic of learning them how to do is left to you. If stuck up you are welcome back...To finish sooner, take your own time....
Nivedithaaaa
- 07-18-2008, 09:13 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Best choice should be the ArrayList, since it's not unordered no need to worried about data retrieving.
- 07-18-2008, 07:11 PM #5
Thanks for those tips. I was in the process of learning Java, here I make a simple program. It however can only read one line, how should I do in order to read all the value from InputFile1.txt? And how to make it to read two textfiles at the same time? Correct me.
Java Code:import java.io.*; public class ReadTextFiles { public static void main(String[] args) throws java.io.IOException { String fileName = "InputFile1.txt"; int i; String inLine; // set up input stream FileReader fr = new FileReader(fileName); // buffer the input stream BufferedReader br = new BufferedReader(fr); // read and display inLine = br.readLine(); // read a line System.out.println(inLine); // display the line br.close(); } }
- 07-18-2008, 07:27 PM #6
Look at how to use loops in a program. There are two primary statements: while and for. Read how to use these statements to repeat a task like reading lines from a file. Also Search for 'for' and 'while' to see sample code.
To read 2 files at the same time, create a second FileReader/BufferedReader, say fr2 and br2
- 07-18-2008, 07:31 PM #7
Member
- Join Date
- Jul 2008
- Posts
- 67
- Rep Power
- 0
K i give you tip how to do that
Java Code:import java.io.*; public class ReadTextFiles { public static void main(String[] args) throws java.io.IOException { String fileName = "InputFile1.txt"; int i; String inLine; // set up input stream FileReader fr = new FileReader(fileName); // buffer the input stream BufferedReader br = new BufferedReader(fr); // read and display String buffer = ""; ArrayList<String> firstFile = new ArrayList<String>(); while ((buffer = br.readLine()) != null) { firstFile.add(buffer); } //Now read the second file or make for this separate method //Read all the lines in array or list //After that you can calculate them. br.close(); } }
- 07-18-2008, 07:46 PM #8
Is it to repeat right?To read 2 files at the same time, create a second FileReader/BufferedReader, say fr2 and br2
Currently I am at my first learning on the input file reader and buffer reader stuff.
Anyway to do it using for loop for reading the values, my code can only read one line of value.
Can I know what is the meaning of these two lines code?
Java Code:String buffer = "";
Java Code:ArrayList<String> firstFile = new ArrayList<String>();
- 07-18-2008, 08:05 PM #9
Member
- Join Date
- Jul 2008
- Posts
- 67
- Rep Power
- 0
Actually if you want read them at the same time you need to use threads at the movement it reads first file and then second.
To read two files i would make method for that with filename parameter, which returns String array.
String buffer = ""; this means you initialize as empty
ArrayList<String> firstFile = new ArrayList<String>(); - this is litlle more advanced but this mostly same as array but its dynamical so you dont need to know the size, in the current code i dont know the size of lines in file, so its best use of it. <String> is generic so we dont need casting and it only stores String type data.
- 07-18-2008, 08:32 PM #10
The tips of your code I used to compile are having two errors which I cant figure out the error problems, newbie needs guide here.Java Code:ReadTextFiles.java:20: cannot find symbol symbol : class ArrayList location: class ReadTextFiles ArrayList<String> firstFile = new ArrayList<String>(); ^ ReadTextFiles.java:20: cannot find symbol symbol : class ArrayList location: class ReadTextFiles ArrayList<String> firstFile = new ArrayList<String>(); ^ 2 errors
- 07-18-2008, 08:48 PM #11
Perhaps save the suggests by jurka until you have the basics down. Save Threads and ArrayList<String> until you can write a simple program to do a simple task. Then learn how to improve it by use of the above.
Here's a sample of what I mean by reading two files at a time:
br1 and br2 are BufferedReaders set up to read from two files just as you defined br in your code above.
The compile errors you got were because the compiler could not find a definition for ArrayList.Java Code:String recFromFile1 = br1.readLine(); // get the next line from file 1 String recFromFile2 = br2.readLine(); // get the next line from file 2
To use a class that is in a package, you need to tell the compiler where the definition for the class is.
That is done by using an import statement. Read the API doc for the class that you want to use and see what package it is in. For ArrayList you should see:at the top of the doc page for ArrayList. To use that package, add:java.util
Class ArrayList<E>
import java.util.ArrayList;
to your program.Last edited by Norm; 07-18-2008 at 08:55 PM.
- 07-18-2008, 10:18 PM #12
I have included import java.util.ArrayList; at the top to add package. The program can run now, but I get these at the output. It didn't show up all the 21 values in my "InputFile1.txt"? It shows something as null? Why?Java Code:import java.util.ArrayList; import java.io.*; public class ReadTextFiles { public static void main(String[] args) throws java.io.IOException { String fileName = "InputFile1.txt"; int i; String inLine; // set up input stream FileReader fr = new FileReader(fileName); // buffer the input stream BufferedReader br = new BufferedReader(fr); // read and display String buffer = ""; ArrayList<String> firstFile = new ArrayList<String>(); while ((buffer = br.readLine()) != null) { firstFile.add(buffer); inLine = br.readLine(); // read a line System.out.println(inLine); // display the line } //Now read the second file or make for this separate method //Read all the lines in array or list //After that you can calculate them. br.close(); } }
Java Code:0.1 4.6 10.3 2.2 4.51 19.1 0.0 -2.1 12 77.1 null
- 07-19-2008, 12:33 AM #13
You are reading 2 times from br but only testing the one in the while() statement for being null.while ((buffer = br.readLine()) != null) {
firstFile.add(buffer);
inLine = br.readLine(); // read a line
System.out.println(inLine); // display the line
It appears the the code reads a line and saves it in firstFile and then reads a second line and prints it and then goes back to read the next line and save it. It seems you are saving every other line.??? You need to rethink what you want to do in this loop.
- 07-19-2008, 01:32 AM #14
Ok, I done it and get the results. Thanks.
Java Code:ArrayList<String> firstFile = new ArrayList<String>(); while ((buffer = br.readLine()) != null) { firstFile.add(buffer); System.out.println(buffer); // display the line }Java Code:1.2 0.1 2.5 4.6 9.1 10.3 12.6 2.2 7.1 4.51 4.6 19.1 10.3 0.0 1.22 -2.1 9.5 12 43 77.1 0.5
- 07-19-2008, 01:43 AM #15
From this created program that can run,
Java Code:import java.util.ArrayList; import java.io.*; public class ReadTextFiles { public static void main(String[] args) throws java.io.IOException { String fileName = "InputFile1.txt"; int i; String inLine; // set up input stream FileReader fr = new FileReader(fileName); // buffer the input stream BufferedReader br = new BufferedReader(fr); // read and display String buffer = ""; ArrayList<String> firstFile = new ArrayList<String>(); while ((buffer = br.readLine()) != null) { firstFile.add(buffer); System.out.println(buffer); // display the line } //Now read the second file or make for this separate method //Read all the lines in array or list //After that you can calculate them. br.close(); } }
I now changed make it so that can read two textfiles before the next step to sum up the values in both text files line by line.
But I got two errors below.Java Code:import java.util.ArrayList; import java.io.*; public class ReadTextFiles { public static void main(String[] args) throws java.io.IOException { String fileName1 = "InputFile1.txt"; String fileName2 = "InputFile2.txt"; int i; String inLine1, inLine2; // set up input stream1 FileReader fr1 = new FileReader(fileName1); // buffer the input stream BufferedReader br1 = new BufferedReader(fr1); // set up input stream2 FileReader fr2 = new FileReader(fileName2); // buffer the input stream BufferedReader br2 = new BufferedReader(fr2); // read and display1 String buffer1 = ""; ArrayList<String> firstFile1 = new ArrayList<String>(); while ((buffer1 = br1.readLine()) != null) { firstFile1.add(buffer1); System.out.println(inLine1); // display the line } br1.close(); //Now read the second file or make for this separate method // read and display2 String buffer2 = ""; ArrayList<String> firstFile2 = new ArrayList<String>(); while ((buffer2 = br2.readLine()) != null) { firstFile2.add(buffer2); System.out.println(inLine2); // display the line } br2.close(); //Read all the lines in array or list //After that you can calculate them. } }
Regarding the while loop, do I need to create another while loop again when additional reading of textfile is implemented? Anyway to make the program shorter?Java Code:ReadTextFiles.java:33: variable inLine1 might not have been initialized System.out.println(inLine1); // display the line ^ ReadTextFiles.java:46: variable inLine2 might not have been initialized System.out.println(inLine2); // display the line ^ 2 errorsLast edited by matt_well; 07-19-2008 at 01:48 AM.
- 07-19-2008, 02:39 AM #16
If you assign a value to the variable it will be initialized and make the compiler happier.variable inLine1 might not have been initialized
If there is another file to read, that would be a way to get all of its contents into your program. I thought that there were only 2 input files.???do I need to create another while loop
Assuming the two files had the same number of records in them, the next loop would be to go thru the two places you stored the input from the files, convert the Strings to numbers, add them and report the results.
- 07-19-2008, 03:53 AM #17
- 07-19-2008, 04:11 AM #18
A couple of techniques to make the compiler happy:
String inLine1 = null; // null value
String inLine1 = ""; // empty string
- 07-19-2008, 04:39 AM #19
Original code,
I tried to initialized both inLine1 and inLine2 which declared in the main part there to either null or "", then I get either all null or all blank.Java Code:String inLine1, inLine2;
Modified code,
orJava Code:String inLine1=null; String inLine2=null;
How come it did not shows the results of values in both textfiles at the outputs there.Java Code:String inLine1=""; String inLine2="";
- 07-19-2008, 09:17 AM #20
Member
- Join Date
- Jul 2008
- Posts
- 67
- Rep Power
- 0
Similar Threads
-
Reading Integers from a text file
By tress in forum New To JavaReplies: 6Last Post: 02-26-2011, 05:45 PM -
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
Applet - reading text files packed into JAR file
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:15 AM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks