Java Programming Tutorial – 21 – Simple Averaging Program

Home/Programming Video Tutorials/Java Video Tutorials/Java Programming Tutorial – 21 – Simple Averaging Program

Java Programming Tutorial – 21 – Simple Averaging Program

FavoriteLoadingAdd to favorites

Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts

Java468x60
By | 2017-06-16T02:16:30+00:00 June 16th, 2017|Java Video Tutorials|50 Comments

50 Comments

  1. Sakhy June 16, 2017 at 2:19 am - Reply

    I didn’t get what the counter++ exaclty does!!!

  2. IamJacksColon4 June 16, 2017 at 2:19 am - Reply

    (average/counter)

  3. Clash with A Square June 16, 2017 at 2:19 am - Reply

    i improved it a little bit

    import java.util.Scanner;
    class ashim{
    public static void main(String args[]){
    Scanner input= new Scanner(System.in);
    int average=0;
    int counter=0;
    int total=0;
    int grade;
    int subject;
    System.out.println("enter the no. that you want to take of average: ");
    subject = input.nextInt();
    System.out.println("enter your numbers turn by turn and press enter : ");
    while(counter<subject){
    grade=input.nextInt();
    total=total+grade;
    counter++;
    }
    average=total/subject;
    System.out.println("the average of your numbers is: "+ average);
    }
    }

  4. JOSEPH SMITH June 16, 2017 at 2:20 am - Reply

    Thanks man, I really love your teaching, please keep up the good work.

  5. Vincent Hwang June 16, 2017 at 2:20 am - Reply

    Successfully made one myself. Did also some tweaks here and there. Thanks!

  6. luz martinez June 16, 2017 at 2:22 am - Reply

    Hi I just want to let you know that you are helping me a lot. I’m studying Java right now and I was completely lost before I found your tutorial.

  7. MaxiumOverdrive June 16, 2017 at 2:23 am - Reply

    Hey, I improved this slightly, it lets you choose how many numbers you are using, eg instead of 10 you can do 30, or even 1,000 if you needed!

    import java.util.Scanner;
    public class average {
    public static void main (String args []) {
    Scanner input = new Scanner(System.in);

    int total = 0, grade, average, counter = 0, numberOfAverage;
    System.out.println("Please enter how many numbers to average");
    numberOfAverage = input.nextInt();

    System.out.println("Please enter " + numberOfAverage + " numbers.");

    while (counter < numberOfAverage) {
    grade = input.nextInt();
    total = grade + total;
    counter++;
    }

    average = total / numberOfAverage;
    System.out.println("The average is: " + average);

    input.close();
    }
    }

  8. Dubis June 16, 2017 at 2:23 am - Reply

    Watching this insired me to make a math exercise program:

    import java.util.Scanner;
    class orange{
    public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    int storage;
    int storage2;
    int storage3;
    int answer;

    System.out.println("Input the first number:");
    storage = input.nextInt();
    System.out.println("Input the second number:");
    storage2 = input.nextInt();
    answer = storage + storage2;
    System.out.println("What is " + storage + " + " + storage2 + "?");
    storage3 = input.nextInt();
    if (storage3 == answer){
    System.out.println("Correct!");
    }else{
    System.out.println("Incorrect…");
    }
    }
    }

    I’m pretty proud!

  9. Lamcis Fark June 16, 2017 at 2:26 am - Reply

    A program with numbers entered feature :

    package javaapplication1;
    import java.util.Scanner;

    public class JavaApplication1{
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double grade,total=0,average;

    System.out.println("Enter first number : (Numbers entered : 0)");
    grade = input.nextDouble();
    total=grade;

    for(int x=1;x<=4;x++){
    System.out.println("Enter next number : (Numbers entered : " + x + ")");
    grade =input.nextDouble();
    total+=grade;
    }
    System.out.println("(Numbers entered : 5)");
    average = total/5;
    System.out.println("Your average is " + average);
    }
    }

  10. Jacob Haig June 16, 2017 at 2:28 am - Reply

    I love to expand upon the examples that are given in my own way. Here’s my example.

    public class Main {
    public static void main(String[] args) {
    AveragerExample.start();
    }
    }

    class AveragerExample {
    public static void start() {
    double counter = 0, sum = 0;
    String newinput;
    boolean istrue = true;
    Scanner input = new Scanner(System.in);
    do {
    newinput = input.next();
    if (newinput.contentEquals("Done")) {
    istrue = false;
    } else {
    try {
    sum += Double.parseDouble(newinput);
    counter++;
    } catch (Exception e) {
    System.out.println("Error 404: " + e);
    }
    }
    } while (istrue);
    System.out.println("Your Average Is: " + sum / counter);
    }
    }

  11. Nika Man June 16, 2017 at 2:29 am - Reply

    3:11? kkk ? Illuminati confirmed

  12. YiHao Xie June 16, 2017 at 2:31 am - Reply

    should have made average a double so it can be a decimal so you dont get an error if the average is a number with a decimal.

  13. Sid Arora June 16, 2017 at 2:31 am - Reply

    how can we take average of N numbers when we don’t know the value of N

  14. sir you Can't do that June 16, 2017 at 2:32 am - Reply

    can someone explain why sumofarray(avg) is not working ???

  15. Xiuxuan Wang June 16, 2017 at 2:33 am - Reply

    I think in an average calculating program you should use "double" instead of "int"

  16. Osama Sany June 16, 2017 at 2:33 am - Reply

    How it is that like counter is less than 10 but you can enter 10 numbers dont it should be 9 numbers that we could enter using that counter loop

  17. ananda wagle June 16, 2017 at 2:34 am - Reply

    what is the meaning of resource leak??

  18. Shritej Chavan June 16, 2017 at 2:36 am - Reply

    He fucked up !!! Average = 5.5!!!! Since average variable is an ‘int’ hence it does not print out 5. Sum of number from 1 to 10 equals to 55. Hence 55/10 = 5.5

  19. עמרי אופיר June 16, 2017 at 2:37 am - Reply

    I did the same as you did and i have and error that says: Scanner cannot be resolved to a type, i dont know what to do please help me 🙁

  20. Basil Yusuf June 16, 2017 at 2:38 am - Reply

    what is the use of total ?

  21. Vibhav Aggarwal June 16, 2017 at 2:38 am - Reply

    he should have used "double average" instead of "int average" for more accurate results

  22. Khalid Mather June 16, 2017 at 2:38 am - Reply

    dude thanks so much , I made my own little minicode based on what ive learnt for the same app :

    import java.util.Scanner;
    public class Blue {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int grade;
    int average;
    int sum = 0;
    int counter = 0;

    System.out.println("Please insert how many classes in total there are : ");
    int total = input.nextInt();

    while (counter < total){
    System.out.println("Please insert the number of kids in class: " + (counter+1));
    grade = input.nextInt();
    sum = sum + grade;

    counter++;

    }

    average = sum/total;
    System.out.println("Your average is :"+ average);

  23. Dr Anubhav Karkar June 16, 2017 at 2:38 am - Reply

    1-10 average cannot be 10

  24. Goel June 16, 2017 at 2:40 am - Reply

    You are so helpful! This tutorial I decided to challenge myself, and once you said what you were gonna do, I paused the video wrote what I though was correct, then watched that part of the video and was correct every time!!! Thank you so much for what you’ve taught me!

  25. 潘蒂懞 June 16, 2017 at 2:41 am - Reply

    one to ten average is ten“““““`Bucky, are you British?

  26. DLZ June 16, 2017 at 2:42 am - Reply

    import java.util.*;
    public class First
    {
    public static void main(String[]args){

    //input from user
    Scanner input = new Scanner(System.in);

    //total for the input numbers added, counter for increment of loop, average is for the answer, numTimes for how many grades the user wants to enter
    int total = 0, counter = 0, average, numTimes;

    System.out.println("Enter the amount of grades you want to enter");

    //How many grades the user wants to input
    numTimes = input.nextInt();

    System.out.println("Now enter the grade numbers!");

    //while loop. While the counter is less than number of times the user inputed, the total will be everything added together that they type, and counter increments it so it doesn’t carry on forever
    while (counter < numTimes) {
    total+=input.nextInt();
    counter ++;
    }
    //The average is all the user’s numbers added together divided by how many numbers they typed
    average = total/numTimes;
    System.out.println("Your average is" + average);
    }
    }

  27. Fransisco Huang June 16, 2017 at 2:43 am - Reply

    5:12 = Eleven hundred? It should be eleven thousand lol.

  28. ܔܛܔ ܔܛܔ June 16, 2017 at 2:43 am - Reply

    When i learn a new lesson, i try to make a program with the new lesson that i learned.

  29. s3rrix iGOD June 16, 2017 at 2:48 am - Reply

    you can also use this as shortcut 😀 write : syso then press control + space (shortcut for System.out.println)

  30. RulingChicken June 16, 2017 at 2:52 am - Reply

    Many complains about it being inefficient.
    If you wanna make it efficient, change every "int" to "double" and remember to change "input.nextInt();" to "input.nextDouble();" and it can count decimals 😉

  31. jRakso June 16, 2017 at 2:53 am - Reply

    Why do you have to use grade mine ran perfectly without it?

  32. Zzzzzz... June 16, 2017 at 2:53 am - Reply

    In python, you can do this in 7 lines.

    how_many=int(input("Enter how many different grades there are:"))
    total=0
    for x in range(how_many):
    grade=int(input("Enter the grade:"))
    total=total + grade
    average=round(total/how_many)
    print("The average of all the grades is", average)

    Amazing.

  33. You Rock June 16, 2017 at 2:54 am - Reply

    Why was i able to achieve this with a shorter code? I am new to java! Please tell me if i wrote the right code?

    public class codetypestest {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);

    double total = 0;

    for (int i=0; i<10; i++)
    {
    double grade = input.nextInt();
    total = total + grade;
    }

    System.out.print("Total of your grades is: " + total);

    double average;
    average = total/10;

    System.out.println(‘n’ + "Average of the entered sum is :" + average );
    }

    }

  34. TheDeceivingEye June 16, 2017 at 2:55 am - Reply

    import java.util.*;
    public class Apples {
    public static void main(String[]args){

    Scanner in = new Scanner(System.in);

    int total = 0, counter = 0, average, numTimes;

    System.out.println("Number of grades to average?…");

    numTimes = in.nextInt();

    while(counter < numTimes){
    total+=in.nextInt();
    counter++;
    }

    average = total/numTimes;
    System.out.println("Average: " + average + ".");

    }

    }

    My custom version, I guess. These tutorials are great, btw!
    (edit: added empty lines to make it more readable.)

  35. Louis Pegu June 16, 2017 at 2:55 am - Reply

    Why did we use the while loop for the counter variable.Didn’t get the logic for the need of counter variable and the while loop @Bucky Roberts

  36. UltraBlizter dep June 16, 2017 at 2:55 am - Reply

    vids 14 15 16 and this is kinda complex

  37. Tomek Siemieniuk June 16, 2017 at 2:56 am - Reply

    3:10
    "H…H…Hey T-t-t-ommy. I…I g-g-ot yo-u-u a n-n-ew ca-car-r."

  38. UltraBlizter dep June 16, 2017 at 2:57 am - Reply

    thx soo much tho! 😀

  39. videovulcan June 16, 2017 at 2:58 am - Reply

    I know it is for demo, but i’d just write:

    total += input.nextInt();
    counter++

    I’ve never understood the point of intermediate variables, perhaps for readability.

  40. davidgpilot June 16, 2017 at 3:00 am - Reply

    You could simply write instead of: "total = total + grade", write "total += grade", and instead of "average = total / 10", write "average = total / counter". Much better now.

    I had to point that out 😛

  41. Jano Privracki June 16, 2017 at 3:01 am - Reply

    Oh! This video is gonna be awesome!

  42. Key Lukin June 16, 2017 at 3:04 am - Reply

    Inside the while statement you can use:
    total += grade;
    instead of
    total = total + grade;

  43. Cabdixakiim Yusuf June 16, 2017 at 3:04 am - Reply

    plzzzz can any one help me how big programs built ..like colors, frames, bottoms…so on
    there is no one can show u how that is built ……waiting you guys plzzzz

  44. sir you Can't do that June 16, 2017 at 3:04 am - Reply

    public class MyKees {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("enter the number of ass : ");

    int hair = input.nextInt();
    int ass[]= new int[hair];

    for( int i= 0 ; i < ass.length; i++){

    System.out.println("array elements " + ass[i]);

    initOneD(ass);
    sumofarray(avg);
    }

    }
    public static void initOneD(int[] array) {
    for (int i = 0; i < array.length; i++) {
    array[i] = (int) (Math.random()*100);
    }
    }

    public static double sumofarray(int[] ass, double average){
    int sum =0;
    for(int i=0; i<ass.length;i++){

    sum= sum+ass[i];

    }
    double avg = sum/ (double) ass.length;

    System.out.println(avg);

    return avg;

    }
    }

  45. Joshua Chenault June 16, 2017 at 3:06 am - Reply

    I think average should be a double here just to be picky. bc 55/10=5.5

  46. Arshad Abrahams June 16, 2017 at 3:08 am - Reply

    How would you output the average if you don’t know how much values the user will give you?

  47. Fahad Hilmi June 16, 2017 at 3:09 am - Reply

    The average should be 5.5 not 5

  48. Saurav Kumar June 16, 2017 at 3:09 am - Reply

    How come "while" was never mentioned previously in the tutorials but now you’re mentioning that we should know of it already? I’m confused! Where is the tutorial that covers while?

  49. sir you Can't do that June 16, 2017 at 3:10 am - Reply

    i tried with both parameters double / int its still the same

  50. Evgeni Kuzmov June 16, 2017 at 3:16 am - Reply

    he said int total hold the sum , but why is it equals to 0 then?

Leave A Comment

*