
I'm trying to make this program write file numbers.dat that's accomplished. I'm also trying to make it write all even numbers into the file and close the file. Then I'm trying to make it re open file and append all odd numbers 1-100 and finally close file.
But for some reason I CANNOT Get the output as I want. I know where the error lays I just can't seem to grasp how to fix it... I've commented as I went along and sought feedback from peers to no luck. I now come here in hopes of assistance.
the problem is within my loop I believe I just cant wrap my head around how to get a proper display.
Current Output shows: Numbers.Dat file name and type Within File Shows: 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056, 1122, 1190, 1260, 1332, 1406, 1482, 1560, 1640, 1722, 1806, 1892, 1980, 2070, 2162, 2256, 2352, 2450, 2550,
Clearly NOT 2,4,6,8,10,12,14,.,.,.Etc
print("package textFileIO;
import java.io.*; import java.util.*;
//class definition public class textFileIO {
//The main function
public static void main(String args[]) {
//This declares a file to open.
File outFile = new File("numbers.dat");
int sumEven = 0;
int sumOdd = 0;
//File IO requires a try/catch block to prevent the program from crashing
try {
//a buffered writer is used to allow us to write to the file.
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
//This for loop handles adding all of the numbers together
for(int i = 1; i < 100; i += 2) {
sumEven += i + 1;
writer.write("" + sumEven + ", ");
}
//adding a new line and closing the file
writer.newLine();
writer.close();
//A buffered Reader is used for reading a new file.
BufferedReader reader = new BufferedReader(new FileReader(outFile));
//Because I only add a new line at the end, I only need to read the first line.
System.out.println(reader.readLine());
//I need to write again, so I close the reader and open the writer.
reader.close();
//you need to create another object to append to the file
//writer.open();
BufferedWriter writer2 = new BufferedWriter(new FileWriter(outFile));
//This for loop is identical to the previous one, except for odd numbers
for(int i = 1; i < 100; i += 2) {
sumOdd += i;
writer2.write("" + sumOdd + ", ");
}
writer.newLine();
writer.close();
//The same here. You need to create another reader
//reader.open();
BufferedReader reader2 = new BufferedReader(new FileReader(outFile));
System.out.println(reader2.readLine());
}
catch (Exception e) {
}
}
}");
With all due respect, Brandon

Hi Friend,
We have modified your code:
import java.io.*;
import java.util.*;
public class textFileIO {
public static void main(String args[]) {
File outFile = new File("numbers.dat");
int sumEven = 0;
int sumOdd = 0;
try{
BufferedWriter writer1 = new BufferedWriter(new FileWriter(outFile,true));
for(int i = 1; i <= 100; i ++) {
if(i%2==0){
writer1.write(""+i+"");
writer1.newLine();
}
}
writer1.close();
BufferedReader br1 = new BufferedReader(new FileReader(outFile));
String str1="";
while ((str1 = br1.readLine()) != null) {
System.out.println(str1);
}
BufferedWriter writer2 = new BufferedWriter(new FileWriter(outFile,true));
for(int i = 1; i <= 100; i ++) {
if(i%2!=0){
writer2.write(""+i+"");
writer2.newLine();
}
}
writer2.close();
BufferedReader br2 = new BufferedReader(new FileReader(outFile));
String str2="";
while ((str2 = br2.readLine()) != null) {
System.out.println(str2);
}
}
catch (Exception e) {}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.