Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem calculating mean and median

$
0
0
I need help fixing the errors in my program. Im not able to calculate the mean and standard deviation and median.
package Statistic;

import eric2.Console;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Statistics {

    static Scanner myScanner;
    static String fileName = "";

    public static void main(String args[]) throws IOException {

        int count;
        double[] theData = null;
        FileReader dataFile;
     

        // create "eric2" console
        Console console = new Console("statistic");
        // and an associated Scanner
        myScanner = new Scanner(System.in);

        // Go open the file
        dataFile = openFile();
        
        // and read the data
        theData = readArray(dataFile);

        // Now a little output, to show that things
        //   worked properly
        useArray(theData, fileName);
       

        // Now sort it, and show the result
        Arrays.sort(theData);

        System.out.println("\n\nThe sorted array:");
        useArray(theData, fileName);
        

        System.out.println("\n\nThank You!");
    }

    private static FileReader openFile() throws IOException {

        FileReader myFile = null;
        int trys = 0;

        // Here's a quick little trick to see
        //   where Java will be looking for files
        //   by default . . .
        String currentDir = System.getProperty("user.dir");
        System.out.println("\nCurrent folder: " + currentDir);
        System.out.print("\nJava will look there for ");
        System.out.println("the file name you enter,");
        System.out.println("     unless you enter the full path . . .\n");

        System.out.print("Enter file name ");
        // Set the default path below
        //   and don't forget to fix this . . .
        // I try using / and it works for my windows and also \\ and it also works.
        System.out.print("(default is C:/Users/Es7eb@n/Documents/NetBeansProjects/numbers-example.txt): ");
        fileName = (String) myScanner.nextLine();

        if (fileName.length() == 0) {
            // Here is where we set the default path
            // I try using / and it works for my windows and also \\ and it also works.
            fileName = "C:/Users/Es7eb@n/Documents/NetBeansProjects/numbers-example.txt";
        }

        // Now we open the file
        while (myFile == null) {
            try {
                myFile = new FileReader(fileName);
            } catch (IOException e) {
                System.err.println("\nCaught IOException: " + e.getMessage());
                trys += 1;
                if (trys > 2) {
                    System.out.println("\nYou had 3 trys . . . sorry -- exiting");
                    System.exit(0);
                }

                System.err.println("\n  Please try again . . .\n");

                System.out.print("Enter file name ");
              
                System.out.print("(default is C:/Users/Es7eb@n/Documents/NetBeansProjects/numbers-example.txt): ");
                fileName = (String) myScanner.nextLine();
                if (fileName.length() == 0) {
                   
                    fileName = " C:/Users/Es7eb@n/Documents/NetBeansProjects/numbers-example.txt";
                }
            }
        }
        return myFile;

    }

    private static double[] readArray(FileReader fin) throws IOException {

        int count;

        // Attach a Scanner to the file
        Scanner numReader = new Scanner(fin);

        // The first number in the file tells how
        //   many numbers will follow
        count = numReader.nextInt();

        // Now that we know how many numbers there
        //   will be, we set up an array of the
        //   right size
        double[] myData = new double[count];

        // And then read in the numbers
        for (int i = 0; i < count; i++) {
            myData[i] = numReader.nextDouble();
        }

        // It's always polite to close a file
        //   after using it
        fin.close();

        return myData;

    }

    private static void useArray(double[] theArray, String fileName) {

        int count = theArray.length;
        System.out.print("\nThe file " + fileName);
        System.out.println(" has " + count + " numbers in it");
        System.out.println("\nThe numbers are:");
        for (int i = 0; i < count; i++) {
            if (i % 10 == 0) {
                System.out.println();

            }


            System.out.print(theArray[i] + " ");
        }

    }

    public double getMean(double[] theArray, String fileName) throws IOException {
  double mean;
    mean = 0.0;
    int sum;
       
  
        for (int i = 0; i < theArray; i++){
            mean += theArray[i];
        }
        
     mean += mean/String fileName;
System.out.println("Mean:" + mean);
        return mean;
    }

    private double getVariance(double[] theArray, double mean) throws IOException {
        double variance;
        variance = 0.0;
               
            for (int i = 0; i < fileName ;i++) {
            }
         variance += (mean - theArray[i] * mean - theArray);
        
            System.out.println("The Variance:" + variance);
        return variance;
    }

    private double getStandard(double variance) throws IOException {
        double Standard;
        Standard = 0.0;
        Standard = Math.sqrt(variance);
        System.out.println("Standard Deviation:" + Standard);
        return Standard;

    }
    private double getMedian(double[] theArray, String fileName) throws IOException {
       double median;
        double[] n = theArray;
       
         median = n * (fileName /2 );
        if (fileName % 2 ==1){
            median = n;
            }
        else {
            
        }
        
           System.out.println ("Median:" + median);
        return median += (theArray[fileName/2 - 1] + theArray[fileName/]/2);
       
    }
            

}



*Eited: "I need help creating a Java program" is a completly useless topic title in this forum

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>