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 3 of 3

Thread: How to sum up elements in an array?

  1. #1
    Junior Member D-X69's Avatar
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to sum up elements in an array?

    hello java programmers out there im new here and also new to java. I just started java programming lately and I find it really tough yet challenging. And I'm trying really hard to get the hang of java, so I hope you pros out there could help me improve.

    ok here's my question.

    Im trying to sum up all elements in an array so i can find its average.


    For example this code:

    int[] testResults = {80, 82, 62, 49, 16, 18};

    How do I sum up all the elements in that array? And how do I find its average?

    Can anyone give me an example of the code and some explanation. Thanks


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to sum up elements in an array?

    Try a for loop.

    If you get stuck, post code! And the (inevitable) cryptic compiler messages, and describe any crazy runtime behaviour - exceptions or just plain wrong answers.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to sum up elements in an array?

    Also at Sum up elements in an array?

    For whatever little it's worth my reply was considered and well intentioned. Ignore it. Move upstream and cast a lure in some other bend in the stream. But that's behaviour that will be noticed.

Similar Threads

  1. [SOLVED] How To Increment Array Elements
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: April 1st, 2012, 12:10 PM
  2. Missing elements in array
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2012, 11:52 PM
  3. Help camparing array elements
    By Richmond in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 12:23 AM
  4. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 09:45 AM
  5. java: adding array elements
    By dambundos in forum Collections and Generics
    Replies: 3
    Last Post: November 4th, 2011, 06:30 AM