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.

Recommended Answers

All 5 Replies

Are you sure the array has non-zero values before the sort?

Yeah I'm sure I just did a print line of each line inside the array and I was given back the values contained inside the text file. It also gives me the average fine. I'm just having a hard time sorting this array for some reason. Is there any other way to sort this array without the .sort function? Using a while loop maybe?

Writing your own sorts is fine for learning how sorts work, it's good exercise, but for real-world coding, you want to use library code wherever possible.
If your call to Arrays.sort() is returning an array with zero values, it's likely that you're doing something wrong. Odds are, you'd have the same problem with your home-made sort routine, which wouldn't be as fast and would likely have bugs that have already been found in the library code. So my suggestion is that you'd do better to figure out why you're coming out with an empty array after calling the library code.

For that, I'd need to see the code where you called sort() and got {0,0,0,0...}


On the other hand, if you want to write your own sorts, you should probably start with bubblesort, then move on to selectionsort and insertionsort, followed by shellsort and quicksort. For ~250 values, bubblesort will be about as fast as anything else, certainly if you count your coding time into the matter, but if you scale it up it'll become very unsatisfactory very quickly, and you'll need to look at the faster and more complex sorts.
These are some of the most widely-taught algorithms known to man, so I don't doubt that you'll find a lot of material with the help of the nice people at google, so to save myself some typing I'll ask you to do the research and try to implement the sort before you ask for more help. :)

Ok thanks for the help. I will look into everything you mentioned. Would it be possible its having problems sorting it because everything in the text file is being treated as a string or does that matter since it's computing the average just fine?

No, since you're using nextInt() to read the data, by the time you're actually dealing with the data it's all ints.

No, since you're able to calculate an average, you're getting actual data. I don't know if you're getting all of the data, since your read stops as soon as the next item is not something that can be parsed as an int, but you're at least getting some data.

Let's see how you called sort() - that'll be where the problem is, I think.

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.