Hi! So I have this program that will read from a file of responses to a survey. The ratings are 1-10 and there are over 250 responses. I have found the average response and also the standard deviation but I also want to find the Median response. To do this I was going to create a method to sort the array.
This is my code:
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;
public class Assignment4
{
public static void main(String[] args)
{
try
{
FileReader reader = new FileReader("StudentResponses.txt");
Scanner in = new Scanner(reader);
FileWriter writer = new FileWriter("FinishedSurvey.out");
PrintWriter out = new PrintWriter(writer);
int MaxSize = 500;
int count = 0;
int [] values = new int[MaxSize];
while(in.hasNextInt())
{
values[count] = in.nextInt();
count++;
}
double average = mean(values, count);
System.out.println("The following number is the average response ");
System.out.println(average);
reader.close();
writer.close();
}
catch(IOException e)
{
System.out.println("Error opening file: " + e);
System.exit(1);
}
}
public static double mean(int [] table, int count)
{
double total = 0;
for(int i = 0; i < count; i++)
total += table[i];
return total/count;
}
public static double StandardDeviation(int [] table, int count)
{
double average = mean(table, count);
double total = 0;
double all;
double total2 = 0;
for(int i = 0; i < count; i++)
{
all = Math.pow(table[i]-average, 2);
total = total + all;
total2 = total/count;
}
double all2 = Math.sqrt(total2);
return all2;
}
}
My question is how would I go about sorting the array because when I sort it using the Array.sort() function I get a list of all 0's. Any help would be appreciated.