简体   繁体   中英

Can someone help explain why I can't display a proper median? [Java]

so the program I am making is supposed to be able to determine the mean, median and how many "grades" have been put into the program. trying to get the median to display and not having any errors using I guess a popular way of finding the median (google thinks its a good idea of how to find the median). When I put in a number I always get 0.0 until I max out my array (set to 25) and then it becomes 1.0. Still not really doing anything math that its supposed to be doing.

  DecimalFormat df = new DecimalFormat ("#0.0##");
   String textBoxInputStr;
   String gCountStr;
   double boxInput = 0;
   double[]gradeArray = new double[25];
   int gradeCount = 0;
   double mean = 0;
   String meanStr;
   double sum = 0;
   double median = 0;
   String medianStr;
   do {    
      try {
         if(gradeCount < 25) {
             textBoxInputStr = JOptionPane.showInputDialog(this,"Enter Grade","Enter Grade", JOptionPane.PLAIN_MESSAGE);  //makes texbox

             boxInput = Double.parseDouble(textBoxInputStr);                //parses what goes on in the textbox

             gradeArray[gradeCount] = boxInput;                           //puts the parsed data into the array 
             gradeCount++;                                                //is the tracker for how many grades are entered
             gCountStr = Integer.toString(gradeCount);                    //converts the counter to string
             txtGrades.setText(gCountStr);                                //places the newly converted counter into grades box for display
          }
         else      //an error
             {      
            JOptionPane.showInputDialog(this,"You can only input 25 values","Too Much Data", JOptionPane.PLAIN_MESSAGE); 
             }
     //(Above) this is all to keep track of how many grades are entered and all the grades are stored in the array.
     //start of median stuff
     Arrays.sort(gradeArray, 0, gradeCount);
  if (gradeCount % 2 == 0) {
      median = ((double)gradeArray[gradeCount/2] + (double)gradeArray[gradeCount/2 - 1])/2;
     medianStr = Double.toString(median);
      txtMedian.setText(medianStr);
  }
  else {
       median = (double) gradeArray[gradeCount/2];
       medianStr = Double.toString(median);
      txtMedian.setText(medianStr);
  }
         //start of the average
               for (double i : gradeArray){
                   sum += i;
               }
               mean = sum / gradeCount;
               meanStr = Double.toString(mean);
               txtMean.setText(meanStr);

       }
     catch(NumberFormatException | HeadlessException e)  //catches bad data
                  {
                  JOptionPane.showMessageDialog(this, "Your input must be numeric!", "Bad Data!", JOptionPane.ERROR_MESSAGE);
                  }
     }
     while(gradeCount <= 25);

      }                                       

Two problems.

First, your do-while loop is missing braces to denote the body. (is this in your original code? or only what you posted here?)

Second, you are declaring gradeArray as a primitive array, but using its length to calculate an updated median every iteration. The length of a primitive array never changes from the time it is instantiated--it is not dependent on how many elements have been added. In other words, gradeArray.length is always 25 (in your example), regardless of how many indices you have actually populated with grades.

To solve the first problem, just add braces around everything after do and before while .

To solve the second problem, you should calculate the median using gradeCount as the "length" of your array, rather than the full (static) length of the primitive array.


EDIT:

...also, I just noticed that your sort is over the full array (so all 25 indices). This may also be causing problems, since the it will consider the "empty" indices (which actually have a default value of 0.0) as part of the elements to sort. Solve this problem in a similar way. Only sort the portion of the array that actually has grades you have entered--ie use this method signature instead. Eg

Array.sort(gradeArray, 0, gradeCount);

SECOND EDIT:

A third problem (that I think is unrelated, but still important to fix) is that the line if (boxInput >= 0) should instead be if (gradeCount < 25) . ...assuming that this line is intended to do what the comment says it does: "makes the check for how many grades are entered".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM