Java Programming Tutorial – 29 – Summing Elements of Arrays

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

24 responses to “Java Programming Tutorial – 29 – Summing Elements of Arrays”

  1. granim32 Avatar

    What are the odds, I put 18,17,16,44,52 as my numbers and they happend to add up to 147. These tutorials are great. Thank you

  2. Ahlen0 Avatar

    All right, it works but I created a method called sumAll because I want to call it later from different class in my application. It sums my numbers user put into array first but if I call it again, it adds previous sum to current sum. I have no idea where I do wrong math.

    private static double sum;
    public static void sumAll() {

    for (int i = 0; i < UserInput.userEnteredValues.length; i++) {
    sum += UserInput.userEnteredValues[i];
    }
    System.out.println("nSum equals = " + sum);

    }

    Example values: 1, 2, 3
    Sum = 6
    Next sum = 12
    Next sum = 18
    How to make it count only array elements over and over?

  3. murdoc Gurung Avatar

    its showing me ""counter cannot be resolved to a variable""
    why???

  4. Temporis Avatar

    class Main {

    public static void main(String[] args) {

    int myArr[] = {10, 10, 10, 10, 10, 10};
    int total = 0;
    int average;

    for (int counter = 0; counter < myArr.length; counter++){
    total += myArr[counter];
    }

    average = total / myArr.length;
    System.out.print(average);

    }
    }

    Made this as a way to find the average of the given numbers in the array!

  5. HATIM DALAL Avatar

    isnt bucky sounding pretty low in this tutorial ?

  6. Premanshu Basak Avatar

    Hi Bucky, first of all thanks for this awesome tutorial. I have a question here… what happens to the counter int variable once the for loop has ended. Can we print or use the counter variable to perform any calculation after the for loop has ended… i.e System.out.println("The total count was : " + counter);

  7. Nadal Derrick Avatar

    I swear you guy yo really cool
    oohh my am beggining to understand java though previously it has been giving me haed time
    You guy big time

  8. Robin Avatar

    why exactly are there brackets around the counter in sum+=bucky[counter] ?

  9. Israel Seka Avatar

    Thanks man your videos are really helpful
    I think you just do one on how to get the larger and smaller number from an array

  10. Liam Lorenzen Avatar

    I don't understand this part: sum+=bucky[counter];

    Why += and why won't it compile if I just + without =… I know that you can *= and /= and so on but I just want an explanation on why the = has to be there :-p
    Thanks

  11. Madeline Laguaite Avatar

    You've basically saved my life multiple times.

  12. AmazingCraft0210 Avatar

    Array averaging program I made

    public class ArrayAverage { public static void main(String[] args) {
      int poop[] = {1,2,3,4,5};
      int total = 0;
      int average;
      
      for(int counter=0;counter<poop.length;counter++){
        total += poop[counter];
      }
      
      
      
      System.out.println("The total is " + total);
      
      average = total / poop.length;
      
      System.out.println("The average is " + average);
      
       }}

  13. Gautam Bhattacharya Avatar

    How can i get data from mysql table and store those columns data in java array to show in html ?

  14. Brandon Valdez Avatar

    BUCKY IS A HERO!!! SAVING ME FOR MY JAVA ONLINE CLASS! U DA MAN!

  15. Darren Maguire Avatar

    Does anyone know where perhaps I could find some JAVA challenges? Challenges that incorporate everything up as far as Array's on this playlist. I'm new to JAVA & wouldn't mind an exercise!

  16. Diana M Avatar

    THANK YOU SO MUCH
    Everywhere i went they gave me such complicated stuff but his was so simple. I can't stress how helpful this was. It saved my mark

  17. Adnan Mir Avatar

    what does '+=' mean?

  18. Kunj Sanghavi Avatar

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at youtube.main(youtube.java:10)

    whats this?

  19. Max Jacobson Avatar

    Here's one way to average the numbers of the array:

    package bucky;

    class apples {
    public static void main(String[] args) {

    int bucky[] = {21, 54, 23, 63, 31};
    double total = 0;

    for(int i = 0; i < bucky.length; i++) {
    total += bucky[i];
    }
    total /= bucky.length;
    System.out.println(total);
    }
    }

  20. Tyler Dameron Avatar

    I feel like bucky is unsung hero of many college students.

  21. Bennington Shrestha Avatar

    how to sum the element in an array by user input……..
    public class SssS {
    public static void main(String[] args) {

    Scanner scan=new Scanner(System.in);
    System.out.println("how many number are you gonna sum?");

    int a=scan.nextInt();
    int[] aa=new int[a];

    for(int i=0;i<=a;i++)
    {
    System.out.println("enter the numbers to be sum:" +(i+1));
    aa[i]=scan.nextInt();

    }

    for (int i=0;i<=a;i++)
    {
    System.out.println("the sum is:");
    int sum=0;
    sum=sum+aa[i];
    System.out.println(aa[i]);
    }

    }
    }
    here's the code……….. need a solution

  22. Skillz0r Avatar

    after you have made the array initializer.. if you write FOR, then press control+space and then enter,, it will create the for loop using your arrayname and a counter named i (instead of counter) this saves you a ton of writing and also no syntax errors!

  23. octavian augustus Avatar

    Check this out, i generalized the situation so that it is not only applicable to dice by changing the number of possible outcomes:

    import java.util.Random;
    import java.util.Scanner;

    class apples {
    public static void main(String[] args){
    Random decide = new Random();
    Scanner input= new Scanner(System.in);
    Scanner inputt= new Scanner(System.in);
    System.out.println("Enter the number of possible outcomes:");
    int poss = inputt.nextInt();
    System.out.println("Enter the number of times you want to throw:");
    int times = input.nextInt();
    int freq[]=new int[poss];
    for (int i=1;i<times+1;i++){
    ++freq[decide.nextInt(poss)];
    }
    System.out.println("FacestFrequencytPerc");
    for(int faces=0;faces<freq.length;faces++){
    System.out.println(faces+1 + "t" + freq[faces] + "t " + freq[faces]*100/times);
    }

    }
    }

  24. tuhin talukder Avatar

    Awesome…thanks for your help.

Leave a Reply

Your email address will not be published. Required fields are marked *