Results 1 to 9 of 9
4Likes
Thread: calculating the mean
 12142012, 12:27 AM #1Member
 Join Date
 Dec 2012
 Posts
 3
 Rep Power
 0
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.
Java 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; } }
Last edited by jimbao; 12142012 at 01:15 AM.
 12142012, 12:35 AM #2AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 6
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(...)I like likes!
 12142012, 01:20 AM #3Member
 Join Date
 Dec 2012
 Posts
 3
 Rep Power
 0
Re: calculating the mean
I edited the thread.
error: '.class' expected
mean = getMean(n[]);
Maybe there's something wrong with the whole getMean method?
 12142012, 01:26 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,715
 Rep Power
 15
Re: calculating the mean
mean = getMean(n[]);
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.
 12142012, 01:31 AM #5AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 6
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 conventionI like likes!
 12142012, 02:04 AM #6Member
 Join Date
 Dec 2012
 Posts
 7
 Rep Power
 0
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[].
 12142012, 02:11 AM #7AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 6
Re: calculating the mean
Yeah, sorry my bad  'n' was just the first input... 'array' has the wrong type...
I like likes!
 12142012, 02:29 AM #8Member
 Join Date
 Nov 2012
 Posts
 36
 Rep Power
 0
Re: calculating the mean
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.....
Java 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; } }
 12142012, 02:40 AM #9Member
 Join Date
 Dec 2012
 Posts
 3
 Rep Power
 0
Similar Threads

Calculating percent
By Dankaru in forum New To JavaReplies: 3Last Post: 11242012, 10:04 PM 
need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09292011, 10:14 PM 
calculating the hypotenuse
By Latanyar in forum New To JavaReplies: 6Last Post: 10122010, 10:20 AM 
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04062010, 06:03 AM 
calculating exponents
By GPB in forum New To JavaReplies: 2Last Post: 03212010, 12:44 PM
Bookmarks