I have an assignment that ask me to read from a file, us an ArrayList to organize and declare the numbers, and then calculate the average of those numbers and print them in a new file. I know that I need 3 parts for this which would be the Reader, Writer and the Array List but i get an error when compiling when I try to read from the scaner. Can someone help with how to read from the file with the ArrayList and likewise, how to write into a new file.
import java.util.ArrayList;
import java.util.Collections;
import java.io.*; //Replaces the scanner
import java.io.BufferedReader;
import java.util.Scanner;
import java.io.FileReader; // Used by the BufferedReader import java.util.Scanner;
import java.io.FileNotFoundException; //
import java.io.IOException; //
class SD9 {
public static void main( String[] args ) {
try{
FileReader Fr = new FileReader( "Patriots.txt" );
// the file reader bridges the program and the .txt file together.
BufferedReader Br = new BufferedReader( Fr );
String line = Br.readLine();
// BufferredReaders can only read one line at a time.
FileWriter fw = new FileWriter( "PatriotsStat.txt" );
BufferedWriter bw = new BufferedWriter( fw );
while( line != null ) {
//BufferredReaders return null once they've reached the end of the file.
ArrayList<Double> Patriots = new ArrayList<Double>();
for(int i = 0; i < 23; ++i ) {
Patriots.add( scan.nextDouble() );
}
/* String Line1 = "2014 PreSeason:";
bw.write( " " );
bw.newLine();
/*String Line3 = " FinalAvg: " + finalAvg;
bw.write( Line3 );
bw.newLine();*/
}
bw.close();
}catch( FileNotFoundException F ) {
//.....
} catch( IOException I ) {
}
}
}