# Read from a text file, then calculate min/max/mode/mean/median...?

• 01-31-2013, 08:59 PM
erichfx
Read from a text file, then calculate min/max/mode/mean/median...?
Hello, I am required to write a program that reads from a program a list of grades, then outputs the min/max/mode/median/mean of these grades. What am I genuinely confused with is where to start....? I understand the logic behind the mathematics calculations, however, I don't understand how to structure my program. I've created an array with a max amount of grades in the file being 500. Do I create new methods for each calculation (ie: public static int min ( ))? I'm confused, any guidance would be appreciated. Here is my code thus far.

Code:

```import java.util.Scanner; import java.io.*; public class statistics {     public static void main (String [] args)     {         System.out.println("What file contains the data?");         Scanner kbd = new Scanner(System.in);         System.out.println("The maximum grade is: ");         System.out.println("The minimum grade is: ");         System.out.println("The mean grade is: ");         System.out.println("The median grade is: ");         System.out.println("The modal grade is: ");     int [] grades = new int [500];     int count = 0;     try     {         input = new Scanner (new File(file));     }     catch(FileNotFoundException e)     {         System.err.println("Error blah blah blah");         System.exit(0);     }     while(input.hasNext())     {         grades[count] = input.NextInt();         System.out.println(grades[count]);         count++;     }     input.close();     Arrays.sort(grades, 0, count);     } }```
• 01-31-2013, 09:25 PM
JosAH
Re: Read from a text file, then calculate min/max/mode/mean/median...?
Quote:

Originally Posted by erichfx
Do I create new methods for each calculation (ie: public static int min ( ))?

Yes, do so; it keeps your code cleaner.

kind regards,

Jos