Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3

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

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

    if (index == 20)

    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 :

    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...
    Last edited by ibrabelware; August 30th, 2012 at 05:34 AM.

Similar Threads

  1. Replies: 3
    Last Post: October 26th, 2011, 03:37 PM
  2. Replies: 3
    Last Post: October 19th, 2011, 07:57 AM
  3. Using recursion to calculate a path through an array
    By jpetticrew in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 20th, 2011, 05:21 PM
  4. Out of bounds error, 2d irregular array of integers
    By Farmer in forum Loops & Control Statements
    Replies: 3
    Last Post: August 1st, 2011, 03:55 PM
  5. recursive program to find median
    By TeamRival in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2011, 10:40 PM

Tags for this Thread