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);
}