Dante Messi Tapia
Dante Messi Tapia

Reputation: 1

Reading from a file using ArrayList in Java

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 ) {
           }

        }
        }

Upvotes: 0

Views: 973

Answers (3)

Cody Maust
Cody Maust

Reputation: 187

The code is failing to compile because your code does not follow the correct Java syntax for a try...catch block

In Java, a try...catch block follows the following form:

try {

    // do something...

} catch (Exception e) {

    // handle the exception...

}

In your code, you will see that you have code in between your try block and your catch block:

The bw.close() line is the culprit.

try {
    // code
}

bw.close();

} catch( FileNotFoundException F ) {
    //.....
} catch( IOException I ) {

}

Assuming you are doing this in an IDE (NetBeans, Eclipse, etc.), this relevant information can be found in the 'Build Output' window

Upvotes: 0

Prashant G
Prashant G

Reputation: 4910

This should work:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

class SD9 {
    public static void main(String[] args) throws FileNotFoundException {

        Scanner scanner = new Scanner(new File("Patriots.txt"));
        PrintWriter writer = new PrintWriter(new File("PatriotsStat.txt"));
        ArrayList<Double> Patriots = new ArrayList<Double>();
        double sum = 0;
        while (scanner.hasNext()) {
            double num = scanner.nextDouble();
            sum += num;
            Patriots.add(num);
        }
        scanner.close();
        for (int i = 0; i < Patriots.size(); i++) {
            writer.write(Patriots.get(i)+"\n");
        }
        double average = sum / Patriots.size();

        writer.write("Average : "+average);
        writer.close();
    }
}

Upvotes: 1

Oğuz K
Oğuz K

Reputation: 400

I believe this will help.

File fr = new File("Patriots.txt");
Scanner sc = new Scanner(fr); 
ArrayList<Double> patriots = new ArrayList<Double>(); 
while(sc.hasNextDouble()){
    patriots.add(sc.nextDouble);
}
sc.close();

Upvotes: 0

Related Questions