# Thread: Find the median of a list of grades in a .txt file

1. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Find the median of a list of grades in a .txt file

Hello folks. I have a program here, that reads a .txt file, that houses an array of grades. I have already coded the MAX, MIN, AVG, amount of grades pertaining to grade letter (i.e A's, B's, C's, D's, and F's apiece) and number of total grades within the array. For those issues it runs perfectly, as it extracts the exam.txt file i have the grades stored in. However, i now have to find the median of the list of grades in the given .txt file. I am having trouble trying to code the median. Here is what i have thus far in it's entirety:

import java.util.*;
import java.io.*;
import java.text.*;

{
//This method produces the heading for the program or you may say that it introduces the program.
public void introduction()
{
System.out.println();
}

public static void main(String[] args) throws Exception
{

double sum = 0;
int MAX = 0;
int MIN = 0;
double AVG = 0;
int gCount = 0;
int gCount1 = 0;
int gCount2 = 0;
int gCount3 = 0;
int gCount4 = 0;

Scanner input = new Scanner(System.in);

System.out.println("***Welcome to the Exam Statistics Program!!***");
System.out.println();

String txtFile = input.next();
Scanner input1 = new Scanner(fr);

input1.close();

for(int g = 0; g< examGrades.length; g++)
{
if(g == 0)
{
MAX = examGrades[g];//I added MAX here too cause it should be here, but everything will be greater than 0.
}

{
break;
}

{
{
}
{
}

{
gCount++;
}

{
gCount1++;
}

{
gCount2++;
}

{
gCount3++;
}

{
gCount4++;
}

}

}

DecimalFormat df = new DecimalFormat("#.##");

System.out.println("Maximun score:" + MAX);
System.out.println("Minimum score:" + MIN);
System.out.println("Average score:" + df.format(AVG));
System.out.println("Number of scores by letter grade, is as follows:");
System.out.println("A:" + gCount);
System.out.println("B:" + gCount1);
System.out.println("C:" + gCount2);
System.out.println("D:" + gCount3);
System.out.println("F:" + gCount4);
// System.out.println("Median of grades entered is:");

}

}

May i please get some assistance of finding the median of a list of numbers in the array? The array is called examGrades if it helps. Thanks.

2. ## Re: Find the median of a list of grades in a .txt file

What trouble does it give you? Do you get an Exception? Some strange behavior? Something else?

Also, please use code tags when posting code: BB Code List - Java Programming Forum - Learn Java Programming

3. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

Ok, i'll remember the code tags from now on, thanks. As for the program, it runs efficiently, with the exception of the median. I had the following, within the for loop:

Java Code:
```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;
}
}```
But that wouldn't even compile, so i commented it out. I have never done median code before, so i am starting from scratch on how to do it.

4. ## Re: Find the median of a list of grades in a .txt file

Why doesn't it compile? Did you try fixing the compilation error?

5. ## Re: Find the median of a list of grades in a .txt file

Finding the median in a naive way takes O(n*log(n)) steps (sorting); google for 'median' and pages show up that explain how to do it in O(n) steps.

kind regards,

Jos

6. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

Thanks, but i have google'd and google'd to no positive results. I have implemented the code above but it states that
Java Code:
`public static double median(double[] m) {`
cannot be resolved to a type.
The median(examGrades.............portion is the part that's highlighted with that notice.

7. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

Sorry,
median(double.....

8. ## Re: Find the median of a list of grades in a .txt file

If you want help, you're going to have to provide an SSCCE that demonstrates exactly what you're doing. We can't really tell what's going on with small snippets of your code.

9. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

Java Code:
```import java.util.*;
import java.io.*;
import java.text.*;

{
//This method produces the heading for the program or you may say that it introduces the program.
public void introduction()
{
System.out.println();
}

public static void main(String[] args) throws Exception
{

double sum = 0;
int MAX = 0;
int MIN = 0;
double AVG = 0;
int gCount = 0;
int gCount1  = 0;
int gCount2 = 0;
int gCount3 = 0;
int gCount4 = 0;

Scanner input = new Scanner(System.in);

System.out.println("***Welcome to the Exam Statistics Program!!***");
System.out.println();

String txtFile = input.next();
Scanner input1 = new Scanner(fr);

input1.close();

for(int g = 0; g< examGrades.length; g++)
{
if(g == 0)
{
MAX = examGrades[g];//I added MAX here too cause it should be here, but everything will be greater than 0.
}

{
break;
}

{
{
}
{
}

{
gCount++;
}

{
gCount1++;
}

{
gCount2++;
}

{
gCount3++;
}

{
gCount4++;
}

}

/*			public static double median(examGrades[] m)
{
int middle = m.length/2;
if (m.length%2 == 1)
{
return m[middle];
}
else
{
return (m[middle-1] + m[middle]) / 2.0;
}
}*/

}

DecimalFormat df = new DecimalFormat("#.#");

System.out.println("Maximun score:" + MAX);
System.out.println("Minimum score:" + MIN);
System.out.println("Average score:" + df.format(AVG));
System.out.println("Number of scores by letter grade, is as follows:");
System.out.println("A:" + gCount);
System.out.println("B:" + gCount1);
System.out.println("C:" + gCount2);
System.out.println("D:" + gCount3);
System.out.println("F:" + gCount4);
//			System.out.println("Median of grades entered is:" + median);

}

}```

10. ## Re: Find the median of a list of grades in a .txt file

Originally Posted by Victor
look here: Selection algorithm - Wikipedia, the free encyclopedia

kind regards,

Jos

11. ## Re: Find the median of a list of grades in a .txt file

I see. It looks like you're trying to define a method inside of your main() method, which you can't do. Put your method at the same level as your main() method, not inside it.

12. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

When you write,
Put your method at the same level as your main() method, not inside it.
......can you tell me exactly what you mean? I am wondering if i should declare the median code within the Grades class section, but outside the main(method)?

13. ## Re: Find the median of a list of grades in a .txt file

Originally Posted by Victor
When you write, ......can you tell me exactly what you mean? I am wondering if i should declare the median code within the Grades class section, but outside the main(method)?
What happened when you tried that?

14. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

When i place the median code in the class before the main() method, it tells me that examGrades, "cannot be resolved to a type."

15. ## Re: Find the median of a list of grades in a .txt file

If you want help, you're going to have to post an SSCCE with updated code.

Did you try googling your error? How do you define an array? How do you declare method parameters? (those are good questions for the basic tutorials) How are you defining the array parameter? What exactly is examGrades?

16. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

Thanks for your help guys, no luck, but oh well. Very much appreciated nonetheless.

17. ## Re: Find the median of a list of grades in a .txt file

So this thread went from an efficient way to calculate the mean of a sequence of numbers to just compilation errors?

kind regards,

Jos

18. Master's Student
Join Date
Jan 2013
Location
Florida
Posts
13
Rep Power
0

## Re: Find the median of a list of grades in a .txt file

I still can't calculate the median properly. I have experimented with where to put the block of code for it, but it isn't working out for me.