Results 1 to 9 of 9
Like Tree4Likes
  • 1 Post By pbrockway2
  • 1 Post By Sierra
  • 1 Post By EngineerVic
  • 1 Post By ralph

Thread: calculating the mean

  1. #1
    jimbao is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default 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 01:15 AM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  3. #3
    jimbao is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: calculating the mean

    I edited the thread.
    error: '.class' expected
    mean = getMean(n[]);
    Maybe there's something wrong with the whole getMean method?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: calculating the mean

    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.
    jimbao likes this.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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
    jimbao likes this.
    I like likes!

  6. #6
    EngineerVic is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default 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[].
    jimbao likes this.

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: calculating the mean

    Yeah, sorry my bad - 'n' was just the first input... 'array' has the wrong type...
    I like likes!

  8. #8
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: calculating the mean

    Quote Originally Posted by jimbao View Post
    Java 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.....

    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;
     
        }
    }
    jimbao likes this.

  9. #9
    jimbao is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: calculating the mean

    Tnx everyone! I'm relieved, I thought there's much more behind it :D Glad to join this community :)

Similar Threads

  1. Calculating percent
    By Dankaru in forum New To Java
    Replies: 3
    Last Post: 11-24-2012, 10:04 PM
  2. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 10:14 PM
  3. calculating the hypotenuse
    By Latanyar in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 10:20 AM
  4. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 06:03 AM
  5. calculating exponents
    By GPB in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 12:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •