I am trying to find a median of an array in Java.
I am getting the exception:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method median(double[]) in the type tez3 is not applicable for the arguments (int[])
at tez3.main(tez3.java:33)
My code is below. What is the problem? How can i print the double function ?
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class tez3 {
public static void main(String args[]) throws java.io.IOException {
Scanner s = new Scanner(new File("C:\\tny\\Deneme1.txt"));
int[] numberList = new int[10];
int i = 0;
int count = 0;
int result = 0;
while (s.hasNextInt()) {
numberList[i++] = s.nextInt();
}
for (i = 0; i < numberList.length; i++) {
System.out.println(+(i + 1) + ".Value: " + numberList[i]);
}
for (i = 0; i < numberList.length; i++) {
count++;
}
for (i = 0; i < numberList.length; i++) {
result += numberList[i];
}
System.out.println("Average of the Values is: " + result / count);
System.out.println("Mode of the Values is: " + mode(numberList));
System.out.println("Median of the Values is: " + median(numberList));
}
public static int mode(int numberList[]) {
int maxValue = 0, maxCount = 0;
for (int i = 0; i < numberList.length; ++i) {
int count = 0;
for (int j = 0; j < numberList.length; ++j) {
if (numberList[j] == numberList[i]) {
++count;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = numberList[i];
}
}
return maxValue;
}
public double median(double[] numberList) {
int factor = numberList.length - 1;
double[] first = new double[(double) factor / 2];
double[] last = new double[first.length];
double[] middleNumbers = new double[1];
for (int i = 0; i < first.length; i++) {
first[i] = numbersList[i];
}
for (int i = numberList.length; i > last.length; i--) {
last[i] = numbersList[i];
}
for (int i = 0; i <= numberList.length; i++) {
if (numberList[i] != first[i] || numberList[i] != last[i]) {
middleNumbers[i] = numberList[i];
}
}
if (numberList.length % 2 == 0) {
double total = middleNumbers[0] + middleNumbers[1];
return total / 2;
} else {
return middleNumbers[0];
}
}
}
The problem is exactly what the error message says. Your method median() takes a double[] as parameter.
But when you're trying to call it you give it numberList as a parameter. But numberList is an int[], that is to say it is not a double[].
The easiest fix is to modify median() so that the parameter it takes is int[] rather than double[].
The problem is that you want to cast all elements in you int[] list to a double. If you don't want to change the parameters of median to int[] then my preferred way to deal with this is to overload the method and convert the arguments
So, it would be something like:
public double median(int[] numberList) {
double[] doubleList = new double[numberList.size()];
for(int i=0; i<doubleList.size(); i++){
doubleList[i] = (double)numberList[i];
}
return median(doubleList);
}
If you did change the parameter of your original 'median' method, then you would just cast the numberList element each time you called it, ie
first[i] = (double) numbersList[i];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.