Java beginner - cant calculate median from an array of integers, please help

Hi, im trying to produce a program that calculates the mean,total and median of an array of 20 numbers entered in via a textfield. The total and mean calculations seem to work fine, but when i put in the calculation for the median everything goes wrong. I'm sure the code is right for calculating the median, but when its entered although it compiles it messes up the the calculations for the total and the mean and produces an incorrect answer itself. Here is the code:

Code Java:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class whileloopq extends Applet implements ActionListener
{
Label label;
TextField input;
int num;
int index;
int[] numArray = new int[20];
int sum;
int total;
double avg;
int median;
public void init ()
{
label = new Label("Enter numbers");
input = new TextField(5);
add(label);
add(input);
input.addActionListener(this);
index = 0;
}
public void actionPerformed (ActionEvent ev)
{
int num = Integer.parseInt(input.getText());
numArray[index] = num;
index++;
if (index == 20)
input.setEnabled(false);
input.setText("");
sum = 0;
for (int i = 0; i < numArray.length; i++)
{
sum += numArray[i];
}
total = sum;
avg = total / index;
Arrays.sort(numArray);
int middle = ((numArray.length) / 2);
if(numArray.length % 2 == 0){
int medianA = numArray[middle];
int medianB = numArray[middle+1];
median = (medianA + medianB) / 2;
} else{
median = numArray[middle + 1];
}
repaint();
System.out.println( Arrays.toString( numArray ) );
}
public void paint (Graphics graf)
{
graf.drawString("Total = " + Integer.toString(total), 25, 85);
graf.drawString("Average = " + Double.toString(avg), 25, 100);
graf.drawString("Median = " + Integer.toString(median), 25, 115);
}
}

It was suggested to me to add the line "System.out.println( Arrays.toString( numArray ) );" to see what was going on and it seems as though the odd numbers are removed from the array. I cant figure it out. Any help or guidance would be very much appreciated.

Thanks

Re: Java beginner - cant calculate median from an array of integers, please help

Can you post the program's output, explain what is wrong with it and show what the output should be?

Add some printlns so the results are on the console where they can be copied.

Re: Java beginner - cant calculate median from an array of integers, please help

First of all, I think there's a problem with this :

I don't think your index will never reach 20 cause an array index start from 0 not from 1.

As Norm said, it will be better if we see your output.

Meanwhile, I just write a small example of what you want to do :

Code java:

import java.util.Arrays;
import java.util.Scanner;
public class Test {
static int numArray[] = new int[10];
static int sum=0;
static double avg=0;
static int median=0;
public static void main(String args[]) {
//filling the array
for(int i=0 ; i<numArray.length ; i++){
System.out.print("Enter a number : ");
Scanner scan = new Scanner(System.in);
numArray[i] = scan.nextInt();
}
//calculating the sum
for(int num : numArray){
sum +=num;
}
System.out.println(sum);
//calculating the average
avg = (double)sum/numArray.length;
System.out.println(avg);
//calculating the median
Arrays.sort(numArray);//we sort the numArray
median = numArray[4];//!\the first element of an array is at the index 0.
System.out.println(median);
}

I tested it and it works.

Hope it can help you...