-
calculating the mean
Java rookie here. I'm trying to write a code which calculates the mean after user inputs some numbers while using array and multiple methods. Main for inputs etc and other one for calculating the mean. That's what I got so far. I can guess there's something massively wrong with declaring variables in getMean();
I would appreciate if someone could point me to the right direction.
Code:
import java.util.Scanner;
public class apples {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("How many values you want to use: ");
//get number
int n = input.nextInt();
//create array with size n
int[] array = new int[n];
//get the input numbers using for loop
for (int m = 0; m < n; m++) {
System.out.print("Value nr " + m + " : ");
array[m] = input.nextInt();
}
System.out.println("Mean is: " + getMean(n));
}
public static double getMean(double[] n){
double sum = 0;
for (int m = 0; m < n.length; m++) {
sum+=n[m];
}
return ((double)sum) / n.length;
}
}
-
Re: calculating the mean
You "guess" something is wrong? How about describing what is actually wrong? What do you expect and what happens instead?
Please indent and wrap your code in the code tags of the forum so it becomes more readable.
And please get your parameter n in your call when you call getMean(...)
-
Re: calculating the mean
I edited the thread.
error: '.class' expected
mean = getMean(n[]);
Maybe there's something wrong with the whole getMean method?
-
Re: calculating the mean
Quote:
mean = getMean(n[]);
That line doesn't exist in the code you posted.
You declared n to be an int. So n[] makes no sort of sense. Ints don't have square brackets after them: they are just numbers.
---
What sort of argument does the getMean() method expect? (A number? an array? something else?). Make sure you pass it the same sort of thing.
-
Re: calculating the mean
"error: '.class' expected"
Give the whole error message including the line where it occurrs - as far as I see you only have one error:
What type is 'n'? What should it be? There you go.
Hint:
- "apples" is a class and then it should be "Apples" by convention
-
Re: calculating the mean
You are passing the wrong argument to getmean(), I think it expects an array not n, that is an int.
Try to pass array but there is also a type mismatch.. getMean() wants double and array is declared as int[].
-
Re: calculating the mean
Yeah, sorry my bad - 'n' was just the first input... 'array' has the wrong type...
-
Re: calculating the mean
Quote:
Originally Posted by
jimbao
Code:
getMean(n);
public static double getMean(double[] n){
double sum = 0;
for (int m = 0; m < n.length; m++) {
sum+=n[m];
}
return ((double)sum) / n.length;
}
}
Your problem lies here.
Firstly, double[] n should be 'int[] n'.... all of your numbers read in are int's so why convert them to an array of doubles?
Secondly, you're passing in a number 'n' when you should be passing in an Array 'array' as that's what you asked for in your method header.....
Code:
import java.util.Scanner;
public class apples {
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("How many values you want to use: ");
//get number
int n = input.nextInt();
//create array with size n
int[] array = new int[n];
//get the input numbers using for loop
for (int m = 0; m < n; m++) {
System.out.print("Value nr " + m + " : ");
array[m] = input.nextInt();
}
System.out.println("Mean is: " + getMean(array));
}
public static double getMean(int[] n){
double sum = 0;
for (int m = 0; m < n.length; m++) {
sum+=n[m];
}
return ((double)sum) / n.length;
}
}
-
Re: calculating the mean
Tnx everyone! I'm relieved, I thought there's much more behind it :D Glad to join this community :)