Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: array trouble

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default array trouble

    I'm trying to populate an array with numbers 1-10 of size 100 and then finding the average of the array and i can't seem to figure out where i'm going wrong. (the multiple methods are required...also i'm not too sure how to correctly use multiple methods. possibly where i'm going wrong.)
    import java.util.Random;
     
    public class wtf 
    {
     
        public static void main(String[] args)
        {
     
          int [] values = new int [100];
           populateArray(values);
     
     
     
     
        }
        public static void populateArray (int []values )
        {
            Random generator = new Random();
            for (int z = 0; z < values.length; z++)
            {
            values[z] = generator.nextInt(10) + 1;
     
     
            }
          average (values);
     
     
        }
     
     
     
        public static void average (int values [] )
        {
    	int sum = 0;
           for (int r = 0; r < values.length; r++)
           {
     
               sum = sum + values [r];
    	   sum = sum/values.length;
     
           }
    	 System.out.println ("The average is " + sum);
     
        }
    }
    Last edited by helloworld922; October 13th, 2010 at 08:55 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: array trouble

    for (int r = 0; r < values.length; r++)
    {
     
    sum = sum + values [r];
    sum = sum/values.length;
     
    }

    this is where your problem lies. if you need more help, post again and i'll elaborate

  3. The Following 2 Users Say Thank You to nemo For This Useful Post:

    JavaPF (October 14th, 2010), littlefuzed (October 13th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: array trouble

    oops, dumb mistake. i see. thanks.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: array trouble

    Please put your code into code tags. See my signature for how to do this.

Similar Threads

  1. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 11:37 AM
  2. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 01:49 PM
  3. Grade Array Trouble
    By kite98765 in forum Collections and Generics
    Replies: 3
    Last Post: January 7th, 2010, 07:14 PM
  4. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM
  5. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM