[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –
Java Programming Tutorial – 29 – Summing Elements of Arrays
by
Tags:
Comments
24 responses to “Java Programming Tutorial – 29 – Summing Elements of Arrays”
-
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
-
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? -
its showing me ""counter cannot be resolved to a variable""
why??? -
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!
-
isnt bucky sounding pretty low in this tutorial ?
-
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);
-
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 -
why exactly are there brackets around the counter in sum+=bucky[counter] ?
-
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 -
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 -
You've basically saved my life multiple times.
-
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);
}} -
How can i get data from mysql table and store those columns data in java array to show in html ?
-
BUCKY IS A HERO!!! SAVING ME FOR MY JAVA ONLINE CLASS! U DA MAN!
-
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!
-
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 -
what does '+=' mean?
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at youtube.main(youtube.java:10)whats this?
-
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);
}
} -
I feel like bucky is unsung hero of many college students.
-
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 -
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!
-
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);
}}
} -
Awesome…thanks for your help.
Leave a Reply