Results 1 to 9 of 9
4Likes Thread: calculating the mean
- 12-13-2012, 11:27 PM #1
Member
- 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; 12-14-2012 at 12:15 AM.
- 12-13-2012, 11:35 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 12-14-2012, 12:20 AM #3
Member
- 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?
- 12-14-2012, 12:26 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: calculating the mean
That line doesn't exist in the code you posted.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.
- 12-14-2012, 12:31 AM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 12-14-2012, 01:04 AM #6
Member
- 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[].
- 12-14-2012, 01:11 AM #7
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: calculating the mean
Yeah, sorry my bad - 'n' was just the first input... 'array' has the wrong type...
I like likes!.gif)
- 12-14-2012, 01:29 AM #8
Member
- Join Date
- Nov 2012
- Posts
- 27
- 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; } }
- 12-14-2012, 01:40 AM #9
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Calculating percent
By Dankaru in forum New To JavaReplies: 3Last Post: 11-24-2012, 09:04 PM -
need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09-29-2011, 09:14 PM -
calculating the hypotenuse
By Latanyar in forum New To JavaReplies: 6Last Post: 10-12-2010, 09:20 AM -
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04-06-2010, 05:03 AM -
calculating exponents
By GPB in forum New To JavaReplies: 2Last Post: 03-21-2010, 11:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks