Question

I am new to programming and for my class we were given an assignment where in java eclipse, we had to write a program that selects a text file(notepad) which has four numbers and computes it's average. We are required to use different methods and I am stuck, I researched everywhere and could not find anything, this is as far as I got and I don't know if I am at all close, the "getTheAverage" method is my issue.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Week04 {
    public static void main(String[] args)     throws IOException {
        String theFile;
        theFile = getTheFileName();
        double theAverage;
    theAverage = getTheAverage(theFile);
        displayTheResult(theAverage,"The average is; ");
}
public static String getTheFileName(){
    String theFile;
    JFileChooser jfc = new JFileChooser();
    jfc.showOpenDialog(null);
    return theFile = jfc.getSelectedFile().getAbsolutePath();
}
public static double getTheAverage(String s) throws IOException{
    double theAverage = 0;
    FileReader fr = new FileReader(s);
    BufferedReader br = new     BufferedReader(fr);
    String aLine;
    while ( (aLine = br.readLine()) != null) {
        theAverage =     Double.parseDouble(s);
    }
    return theAverage;
}
public static void displayTheResult(double x,     String s){
        JOptionPane.showMessageDialog(null,s + x);
}
}
Was it helpful?

Solution 2

Assuming that "average" indicates the arithmetic mean, you need to sum all the values parsed and then divide by the number of values you found. There are at least 4 problems with your code in this regard:

  • Double.parseDouble() is reading from the uninitialized variable s, instead of the value of the line you just read (which is in aLine)
  • You are not summing the values found
  • You are not keeping a tally of the NUMBER of values found
  • You are not dividing the sum by the tally

Based on your code, here's an example of how you might have done it.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Week04 {
    public static void main(String[] args) throws IOException {
        String theFile;
        theFile = getTheFileName();
        double theAverage;
        theAverage = getTheAverage(theFile);
        displayTheResult(theAverage,"The average is; ");
    }

    public static String getTheFileName() {
        String theFile;
        JFileChooser jfc = new JFileChooser();
        jfc.showOpenDialog(null);
        return theFile = jfc.getSelectedFile().getAbsolutePath();
    }

    public static double getTheAverage(String s) throws IOException {
        double value = 0, numValues = 0;
        FileReader fr = new FileReader(s);
        BufferedReader br = new BufferedReader(fr);
        String aLine;
        while ( (aLine = br.readLine()) != null) {
            if (aLine.equals("")) continue;
            value += Double.parseDouble(aLine);
            numValues++;
        }
        if (numValues > 1) {
            return value/numValues;
        } else {
            return value;
        }

    }
    public static void displayTheResult(double x, String s){
            JOptionPane.showMessageDialog(null,s + x);
    }
}

OTHER TIPS

Try using a Scanner object instead. It seems like you are making this more difficult than it has to be.

    // Get file name from user.
    Scanner scnr = new Scanner(System.in);
    System.out
            .println("Please enter the name of the file containing numbers to use?");
    String fileName = scnr.next();
    scnr.close();

    // Retrieve File the user entered
    // Create File object
    File file = new File(fileName);
    try {

        // Create new scanner object and pass it the file object from above.
        Scanner fileScnr = new Scanner(file);

        //Create values to keep track of numbers being read in
        int total = 0;
        int totalNumbers = 0;

        // Loop through read in file and average values.
        while (fileScnr.hasNext()) {
            total += fileScnr.nextInt();
            totalNumbers++;
        }
        //Average numbers
        int average = total/totalNumbers;

        // Close scanner.
        fileScnr.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        // Quit Program if file input is bad.
        System.exit(0);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top