anyone enlighten me? i have to find the median...below i found the mean and read the question wrong. how do calculate the median? i have my text input file which has 7 ints, 1-7, so i kno #3 will show up for median...but i dont know how to code it...pls help!

import java.util.*;
import java.io.*;

class median{


    public static void main(String[] args) throws Exception{
    ArrayList<Integer> store = new ArrayList<Integer>();
    String filename;
    System.out.println("Please type in the name of the file");
    Scanner readerUsr = new Scanner(System.in);
    filename = readerUsr.nextLine();
    File inputFile = new File("int.txt");

    Scanner reader = new Scanner(inputFile);

    while(reader.hasNext()){
    store.add(Integer.parseInt(reader.nextLine()));
    }

    calculateSum(store);

    }

    static void calculateSum(ArrayList<Integer> myArray){

    Iterator<Integer> iterator = myArray.iterator();
     int sum=0;
     int n=0;

    while(iterator.hasNext()){
     iterator.next();
     sum += iterator.next();
     n++;

    }
    double getAverage = sum/n;
    System.out.println("The Average is " + getAverage + "");


    }
    }

Could you please post your input text file?

Also I believe you can use the method get() from the ArrayList to retrieve an object at a specific index, therefore I think you can use something like:

median = myArray.get(medianIndex);

Thanks,
Komyg

You give no indication that you understand what you are trying to do. You should know that the median is defined as the central value in a set. If the set has an even number of elements it is the arithmetic mean of the central two. If you order the values in the array it can be found by taking the central index. (3 in the example). The task then becomes, sort the array and determine the central value(s).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.