|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-22-2008, 05:12 AM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
|
Program can run but output all null
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=null;
String inLine2=null;
// 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> File1 = new ArrayList<String>();
while ((buffer1 = br1.readLine()) != null) {
File1.add(buffer1);
System.out.println("File1 values: "+ inLine1); // display the line
}
br1.close();
//Now read the second file or make for this separate method
// read and display2
String buffer2 = "";
ArrayList<String> File2 = new ArrayList<String>();
while ((buffer2 = br2.readLine()) != null) {
File2.add(buffer2);
System.out.println("File2 values: " + inLine2); // display the line
}
br2.close();
//Read all the lines in array or list
//After that you can calculate them.
}
}
Creating this simple program but get messed up previously.
What is the mistake in this program? Correct me.
|
|

07-22-2008, 06:34 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
import java.util.ArrayList;
import java.util.List;
import java.io.*;
public class ReadTextFilesRx
{
public static void main(String[] args) throws java.io.IOException
{
String fileName1 = "InputFile1.txt";
String fileName2 = "InputFile2.txt";
// These are not used below.
// int i;
// String inLine1=null;
// String inLine2=null;
// set up input stream1
FileReader fr1 = new FileReader(fileName1);
// buffer the input stream
BufferedReader br1 = new BufferedReader(fr1);
// read and display1
String buffer1 = "";
List<String> File1 = new ArrayList<String>();
while ((buffer1 = br1.readLine()) != null) {
File1.add(buffer1);
// System.out.println("File1 values: "+ inLine1);
System.out.println("File1 values: "+ buffer1);
}
br1.close();
//Now read the second file or make for this separate method
// read and display2
// set up input stream2
FileReader fr2 = new FileReader(fileName2);
// buffer the input stream
BufferedReader br2 = new BufferedReader(fr2);
String buffer2 = "";
List<String> File2 = new ArrayList<String>();
while ((buffer2 = br2.readLine()) != null) {
File2.add(buffer2);
// System.out.println("File2 values: " + inLine2);
System.out.println("File2 values: " + buffer2);
}
br2.close();
//Read all the lines in array or list
System.out.println("File1 = " + File1);
System.out.println("File2 = " + File2);
//After that you can calculate them.
}
}
|
|

07-22-2008, 02:04 PM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Thanks hardwired for the useful guides.
For newbie, I have difficulties in ArrayList<String> when dealing to parse the values for calculation.
I wish to sum the values in "InputFile1.txt" and "InputFile1.txt" line by line to gives the output.
InputFile1 InputFile2 TotalSum
1.2 1.3 2.5
0.1 9.1 9.2
2.5 2.9 5.4
4.6 0.6 5.2
9.1 9.1 18.2
10.3 10.8 21.1
12.6 17.6 30.2
2.2 22.2 24.4
7.1 7.11 14.21
4.51 3.51 8.02
4.6 0.6 5.2
19.1 9.1 28.2
10.3 0.3 10.6
0.0 0.01 0.01
1.22 1.22 2.44
-2.1 -2.1 -4.2
9.5 9.51 19.01
12 2 14
43 4.3 47.3
77.1 7.1 84.2
0.5 0.5
Where to create loop or while loop to parse those values?
I created a simple for loop but not sure bout it,
for(int i=0;i<?;i++){
double total[] = new double[i];
double values1[] = new double[i];
double values2[] = new double[i];
values1[i] = Double.parseDouble(buffer1);
values2[i] = Double.parseDouble(buffer2);
total[i] = values1[i] + values2[i];
}
The value "?" should be set to what so that it will be something likes buffer=""
I got also got this error when I put the for loop after the line "br2.close();"
ReadTextFilesRx.java:68: reached end of file while parsing
}→
^
1 error
How to create a useful 'for loop' for every list value and then parse it because need to get every total.
Last edited by matt_well : 07-22-2008 at 02:18 PM.
|
|

07-22-2008, 05:57 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
ReadTextFilesRx.java:68: reached end of file while parsing
Could you copy and paste the full text of the error message. Don't edit it any.
You need to post your code for around the area where the error occurred- line 68
|
|

07-22-2008, 11:37 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
I have difficulties in ArrayList<String> when dealing to parse the values for calculation
Since you had ArrayLists to hold the input/read-in data I assumed that you wanted to read in all the data from the two files, close the files/readers and then process the data later.
This is an elegant approach but there are other ways you can do it.
The "<String>" part is a generic type parameter. Without these or "@Suppress" statements we get compiler warnings (which are unpleasant). See Lesson: Generics for more about generics.
for(int i=0;i<?;i++){
double total[] = new double[i];
double values1[] = new double[i];
double values2[] = new double[i];
values1[i] = Double.parseDouble(buffer1);
values2[i] = Double.parseDouble(buffer2);
total[i] = values1[i] + values2[i];
}
The value "?" should be set to what so that it will be something likes buffer=""
This is handily solved with the ArrayList approach. Another option is to read the files once to get the number of lines, allocate the arrays, ie, with the number of lines in each file, and read them again to load the data into the arrays. Another way is to start with zero-length arrays and add an element on the fly as you read each file line. You can use System.arraycopy for this last way.
I got also got this error when I put the for loop after the line "br2.close();"
Code:
ReadTextFilesRx.java:68: reached end of file while parsing
}→
^
1 error
Looks like you may have missed/misplaced a curley brace.
import java.io.*;
import java.util.*;
public class ReadTextFilesRx
{
public static void main(String[] args) throws java.io.IOException
{
String fileName1 = "InputFile1.txt";
String fileName2 = "InputFile2.txt";
// Read files and collect data for processing:
List<String> file1Data = getData(fileName1);
List<String> file2Data = getData(fileName2);
// Let's see what we have in the lists:
System.out.println("file1Data = " + file1Data);
System.out.println("file2Data = " + file2Data);
// Now it's easy to get the number of data items:
System.out.println("file1Data size = " + file1Data.size());
System.out.println("file2Data size = " + file2Data.size());
//After that you can calculate them.
System.out.printf("%s %s %s%n",
"InputFile1", "InputFile2", "Totals");
int maxSize = Math.max(file1Data.size(), file2Data.size());
for(int i = 0; i < maxSize; i++)
{
String data1 = getNextValue(file1Data, i);
String data2 = getNextValue(file2Data, i);
double one = parseValue(data1);
double two = parseValue(data2);
System.out.printf("%7s %12s %12.2f%n",
data1, data2, one + two);
}
}
private static List<String> getData(String filePath) throws IOException
{
List<String> list = new ArrayList<String>();
FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);
String buffer;
while( (buffer = br.readLine()) != null)
{
list.add(buffer);
}
br.close();
return list;
}
private static String getNextValue(List<String> list, int index)
{
if(index > list.size()-1) // gone past end of list
return "";
return list.get(index);
}
private static double parseValue(String s)
{
if(s.equals("")) // missing data values appear as empty string
return 0;
return Double.parseDouble(s);
}
}
|
|

07-23-2008, 09:16 AM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Thanks hardwired.
I tried to get the printed results into a textfile call "results.txt", I have created a FileWriter code and put it below at the main part after the line System.out.printf("%7s %12s %12.2f%n", data1, data2, sum);
I get the sum become,
FileWriter fw = new FileWriter("C:\\results.txt");
BufferedWriter bw = new BufferedWriter(fw);
String Sum = Double.toString(sum);
bw.write(Sum);
The program can be compiled with no errors but I get an empty "results.txt" file in my C drive after that.
In future, if I wish to save the "C:\\results.txt" into the same directory as the program location, I should change the "C:\\" to what?
In java, is there anyway to get the overall printed results into the "results.txt" directly?
To make the results with index appear to be in the "results.txt" file be something like this below,
Index InputFile1 InputFile2 TotalSum
0 1.2 1.3 2.5
1 0.1 9.1 9.2
2 2.5 2.9 5.4
3 4.6 0.6 5.2
4 9.1 9.1 18.2
5 10.3 10.8 21.1
6 12.6 17.6 30.2
7 2.2 22.2 24.4
8 7.1 7.11 14.21
9 4.51 3.51 8.02
10 4.6 0.6 5.2
11 19.1 9.1 28.2
12 10.3 0.3 10.6
13 0.0 0.01 0.01
14 1.22 1.22 2.44
15 -2.1 -2.1 -4.2
16 9.5 9.51 19.01
17 12 2 14
18 43 4.3 47.3
19 77.1 7.1 84.2
20 0.5 0.5
Last edited by matt_well : 07-23-2008 at 09:29 AM.
|
|

07-23-2008, 09:37 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
To save the file in the same location as the project you working on, change the following code,
FileWriter fw = new FileWriter("C:\\results.txt");
as follows,
FileWriter fw = new FileWriter("results.txt");
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-23-2008, 09:57 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
Try this in place of the last section in the main method above:
//After that you can calculate them and write to file.
String path = "readTextFilesResults.txt";
PrintWriter pw = new PrintWriter(
new FileOutputStream(path));
pw.printf("%s %s %s%n",
"InputFile1", "InputFile2", "Totals");
int maxSize = Math.max(file1Data.size(), file2Data.size());
for(int i = 0; i < maxSize; i++)
{
String data1 = getNextValue(file1Data, i);
String data2 = getNextValue(file2Data, i);
double one = parseValue(data1);
double two = parseValue(data2);
pw.printf("%7s %12s %12.2f%n",
data1, data2, one + two);
}
pw.close();
|
|

07-23-2008, 10:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
Craig, I want to know one thing, actually it is off topic with this thread, but not much valid to start a new.
I've seen that in lots of such similar cases you use PrintWriter, rather FileWriter. Any reason for that?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-23-2008, 10:21 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
Actually, I usually use
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(
because it will write about anything. Same for the reader correlates.
I generally do not use FileWriter because it is more limited/fragile. It is a convenience class for text. It's good to use for reading into JTextArea.
Above I used PrintWriter because it matched up nicely with the System.out.printf statements in place. I couldn't find a BufferedWriter arrangement/wrapper for it.
|
|

07-23-2008, 10:37 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
I agreed with you mate. Normally I use FileWriter for text file manipulations in small size applications. To working with larger files I used one of them you mentioned above.
I really used PrinWriter so far, because I'm not much familiar with it. Actually nothing much to worried about it.
Anyway, thanks for the explanation. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-23-2008, 12:37 PM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Ok, get it hardwired.
How to make the last value of output at InputFile2 there to become "0" where in future any "" no value to become "0"?
Index InputFile1 InputFile2 TotalSum
0 1.2 1.3 2.5
1 0.1 9.1 9.2
2 2.5 2.9 5.4
3 4.6 0.6 5.2
4 9.1 9.1 18.2
5 10.3 10.8 21.1
6 12.6 17.6 30.2
7 2.2 22.2 24.4
8 7.1 7.11 14.21
9 4.51 3.51 8.02
10 4.6 0.6 5.2
11 19.1 9.1 28.2
12 10.3 0.3 10.6
13 0.0 0.01 0.01
14 1.22 1.22 2.44
15 -2.1 -2.1 -4.2
16 9.5 9.51 19.01
17 12 2 14
18 43 4.3 47.3
19 77.1 7.1 84.2
20 0.5 0.5
Make into this,
Index InputFile1 InputFile2 TotalSum
0 1.2 1.3 2.5
1 0.1 9.1 9.2
2 2.5 2.9 5.4
3 4.6 0.6 5.2
4 9.1 9.1 18.2
5 10.3 10.8 21.1
6 12.6 17.6 30.2
7 2.2 22.2 24.4
8 7.1 7.11 14.21
9 4.51 3.51 8.02
10 4.6 0.6 5.2
11 19.1 9.1 28.2
12 10.3 0.3 10.6
13 0.0 0.01 0.01
14 1.22 1.22 2.44
15 -2.1 -2.1 -4.2
16 9.5 9.51 19.01
17 12 2 14
18 43 4.3 47.3
19 77.1 7.1 84.2
20 0.5 0 0.5
|
|

07-23-2008, 05:56 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
In getNextValue return"0" instead of "" when index is greater than the last index of the list.
Since there are no blank/empty strings to deal with you can dispense with the parseValue method and parse the strings in the for loop.
double one = Double.parseDouble(data1);
double two = Double.parseDouble(data2);
|
|

07-23-2008, 07:15 PM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Hi, something extra I wana ask.
The two original files are as follow:
InputFile1.txt
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
InputFile2.txt
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
If the reading of InputFile2 is read with a "delay of three" steps index than InputFile1, which mean when reading of InputFile2 happen, there will be three "0" to read first before the reading continue. The InputFile2 is not modified. How to make it in this case?
Imagination reading of InputFile2,
0
0
0
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
Last edited by matt_well : 07-23-2008 at 07:17 PM.
|
|

07-23-2008, 07:49 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
One way you could do this is to add the extra values to the list after reading the file.
The ArrayList class api has a method we can use:
// Add 3 values to beginning of file2Data.
for(int i = 0; i < 3; i++) {
file2Data.add(0, "0");
}
|
|

07-24-2008, 10:48 AM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Ok, this other thing, it is not delay of "0" at the input reading there but it is occur at the middle.
In this case, for further calculation propose, is there anyway to create a method so that after the line "String data2 = getNextValue(file2Data, i);" there will be a method to delay the reading by addition of three "0" to read first before the reading continue and after that continue the calculation.
for(int i = 0; i < maxSize; i++)
{
String data1 = getNextValue(file1Data, i);
String data2 = getNextValue(file2Data, i);
double one = parseValue(data1);
double two = parseValue(data2);
pw.printf("%7s %12s %12.2f%n",
data1, data2, one + two);
}
The two input files "InputFile1" and "InputFile2" still remain unchange, just disturb by adding three "0" at the beginning of InputFile2 at the middle of the process before the calculation.
The output file will become:
Index InputFile1 InputFile2 Totals
0 1.2 0 1.20
1 0.1 0 0.10
2 2.5 0 2.50
3 4.6 1.3 5.90
4 9.1 9.1 18.20
5 10.3 2.9 13.20
6 12.6 0.6 13.20
7 2.2 9.1 11.30
8 7.1 10.8 17.90
9 4.51 17.6 22.11
10 4.6 22.2 26.80
11 19.1 7.11 26.21
12 10.3 3.51 13.81
13 0.0 0.6 0.60
14 1.22 9.1 10.32
15 -2.1 0.3 -1.80
16 9.5 0.01 9.51
17 12 1.22 13.22
18 43 -2.1 40.90
19 77.1 9.51 86.61
20 0.5 2 2.50
21 0 4.3 4.30
22 0 7.1 7.10
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|