0

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

        }
        }
7
  • 1
    Declare your ArrayList outside the while loop please. Commented Apr 17, 2015 at 4:49
  • Patriots.add(Double.parseDouble(line) ; // assuming your file only has the numbers Commented Apr 17, 2015 at 4:50
  • Do u want to compute average for all numbers on each line ? or is your input file having only one number per line ?? Or do u want the average of all the nos in the whole file ? Commented Apr 17, 2015 at 4:52
  • You seem to be calling scan.nextDouble() but I don't see you creating a Scanner object? Commented Apr 17, 2015 at 4:52
  • Awesome thanks. The file I am reading from also has letters...how can i make the Patriots.add for numbers and strings? and i want to compute the average for lines seperatley. For example the file i read from has the name of a team and their scores fro a season. This includes preseason,regular season, and post season games. I have to calculate the average for preseason,regular season, and post season games seperately. Commented Apr 17, 2015 at 4:55

3 Answers 3

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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();

3 Comments

Thanks!!!!What if the file I am reading froma also has letters? I would be needing strings and doubles. How do I do that with array List.
Then, you can read them all as String using next() or nextLine() methods. Then you can use parseDouble() method for doubles.
How would the syntax look like?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.