asked on # How to locate and display the Median value of an Array

I need to create a program that has 9 values in an array they must be sorted in ascending order then displayed. The program is to then find the median value and display it as well. I think that I almost have it, but I am getting a lot of errors. Any help is extremely appreciated.

```
import java.util.Collections;
import java.util.Arrays;
public class Median
{
public static void main(String[] args)
{
int[] arrayNum = new int[] {600, 59, 754, 169, 420, 699, 1000, 800, 999};
int numSort = arrayNum.length - 1;
for(a = 0; a < arrayNum.length - 1; ++a)
{
for(b = 0; b < numSort; ++b)
{
if(arrayNum[b] > arrayNum[b + 1])
{
temp = arrayNum[b];
arrayNum[b] = arrayNum[b + 1];
arrayNum[b + 1] = temp;
System.out.println("Numbers in the array = " + arrayNum[b]);
}
}
--numSort;
}
}
public static double median(double[] m)
{
int middle = m.length/2;
if (m.length%2 == 1) {
return m[middle];
} else
{
return (m[middle-1] + m[middle]) / 2.0;
System.out.println("Median number in the array = " + middle[b]);
}
}
}
```

Java

Even after you declare

int b in the for() loop and then int temp

you still have abigger problem because you want to use varaible b in the place where it is out of scope

here:

int b in the for() loop and then int temp

you still have abigger problem because you want to use varaible b in the place where it is out of scope

here:

```
System.out.println("Median number in the array = " + middle[b]);
```

This is the similar question with accepted solution:

https://www.experts-exchange.com/Programming/Languages/Java/Q_27081221.html?sfQueryTermInfo=1+10+30+median+yan

https://www.experts-exchange.com/Programming/Languages/Java/Q_27081221.html?sfQueryTermInfo=1+10+30+median+yan

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst

William Peck

I changed the a to Int a, b into int b, and temp into int temp.

Would this be correct?

System.out.println("Median number in the array = " + [m]middle);

Would this be correct?

System.out.println("Median

Post your new code - I'll check .

Ok, so the problems were:

1. you didnt declare, a, b and temp.

2. in median you were using wrong variables.

3. In median function, you were returning before System.out call

4. you did not call median function

The corrected code works perfectly.

Here is the corrected code:

1. you didnt declare, a, b and temp.

2. in median you were using wrong variables.

3. In median function, you were returning before System.out call

4. you did not call median function

The corrected code works perfectly.

Here is the corrected code:

```
import java.util.Collections;
import java.util.Arrays;
public class Median
{
public static void main(String[] args)
{
int[] arrayNum = new int[] {600, 59, 754, 169, 420, 699, 1000, 800, 999};
int numSort = arrayNum.length - 1;
int temp = 0;
for(int a = 0; a < arrayNum.length - 1; ++a)
{
for(int b = 0; b < numSort; ++b)
{
if(arrayNum[b] > arrayNum[b + 1])
{
temp = arrayNum[b];
arrayNum[b] = arrayNum[b + 1];
arrayNum[b + 1] = temp;
System.out.println("Numbers in the array = " + arrayNum[b]);
}
}
--numSort;
}
System.out.println("Median number in the array = " + median(arrayNum));
}
public static double median(int[] m)
{
int middle = m.length/2;
if (m.length%2 == 1) {
return m[middle];
}
else{
return (m[middle-1] + m[middle]) / 2.0;
}
}
}
```

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

Here is the new code, Thanks for that link btw. I was actually thinking about using the array list when I was first writing it, but I didn't know if it would work right.

```
import java.util.Collections;
import java.util.Arrays;
public class Median
{
public static void main(String[] args)
{
int[] arrayNum = new int[] {600, 59, 754, 169, 420, 699, 1000, 800, 999};
int numSort = arrayNum.length - 1;
for(int a = 0; a < arrayNum.length - 1; ++a)
{
for(int b = 0; b < numSort; ++b)
{
if(arrayNum[b] > arrayNum[b + 1])
{
int temp = arrayNum[b];
arrayNum[b] = arrayNum[b + 1];
arrayNum[b + 1] = temp;
System.out.println("Numbers in the array = " + arrayNum[b]);
}
}
--numSort;
}
}
public static double median(double[] m)
{
int middle = m.length/2;
if (m.length%2 == 1) {
return m[middle];
} else
{
return (m[middle-1] + m[middle]) / 2.0;
System.out.println("Median number in the array = " + [m]middle);
}
}
}
```

As you see, you did not call the median function:

and also, System.out.println("Median number in the array = " + [m]middle);

would not be correct as the syntax is wrong!

Also, the variable middle isnt accessible in the median function.

and also, System.out.println("Median

would not be correct as the syntax is wrong!

Also, the variable middle isnt accessible in the median function.

oops, i misunderstood :P

m[middle] will work :)

But as in the else, you want the sum, you will have to save it in another variable.

Try and run the code i submitted, it works ;)

m[middle] will work :)

But as in the else, you want the sum, you will have to save it in another variable.

Try and run the code i submitted, it works ;)

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!

Walt Forbes

Thank you for the post manishkungwani. Just one problem it only seems to be displaying the first 6 values and reapeats them.

Log in or sign up to see answer

Become an EE member today7-DAY FREE TRIAL

Members can start a 7-Day Free trial then enjoy unlimited access to the platform

or

Learn why we charge membership fees

We get it - no one likes a content blocker. Take one extra minute and find out why we block content.

Not exactly the question you had in mind?

Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.

ask a question
It is in your Screenshot I have the values listed as [59, 169, 420, 600, 699, 754, 800, 999, 1000]

it is displaying as 59, 169, 420, 699, 800, 999, 169, 420. It is missing the values 800 and 1000.

it is displaying as 59, 169, 420, 699, 800, 999, 169, 420. It is missing the values 800 and 1000.

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

sorry the values missing are 754/100.

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

You both are amazing! Thank you so much! I see my errors and I have learned a great deal from my mistakes and your guys help! :) For yan I especially thank you for all your help in all of my other previous questions you are truly an asset to this community and both of you are extremely appreciated.

Thanx :)

Some advice, for practical purposes, its recommended that you use the available functions in Java for sorting, those are much more efficient and useful..

Arrays are for classrooms ;)

Some advice, for practical purposes, its recommended that you use the available functions in Java for sorting, those are much more efficient and useful..

Arrays are for classrooms ;)

Your help has saved me hundreds of hours of internet surfing.

fblack61

Thanks!

Pleasant to read your kind words.

Pleasant to read your kind words.

Great advice and duly noted!

Most welcome :)

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

for(a = 0; a < arrayNum.length - 1; ++a)

should be

for(int a = 0; a < arrayNum.length - 1; ++a)

the same thing about variables b and temp