Unable to pass an array to a second method











up vote
2
down vote

favorite












I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;



import java.util.Random;

public class ArrayTest{

public static void main(String args){

Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}

public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}


Where am I going wrong, be gentle.










share|improve this question




















  • 1




    You never call the average function.
    – Matt
    Nov 11 at 0:39















up vote
2
down vote

favorite












I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;



import java.util.Random;

public class ArrayTest{

public static void main(String args){

Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}

public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}


Where am I going wrong, be gentle.










share|improve this question




















  • 1




    You never call the average function.
    – Matt
    Nov 11 at 0:39













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;



import java.util.Random;

public class ArrayTest{

public static void main(String args){

Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}

public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}


Where am I going wrong, be gentle.










share|improve this question















I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;



import java.util.Random;

public class ArrayTest{

public static void main(String args){

Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}

public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}


Where am I going wrong, be gentle.







java arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:02









GBlodgett

7,14541329




7,14541329










asked Nov 11 at 0:22









Josh Sanders

133




133








  • 1




    You never call the average function.
    – Matt
    Nov 11 at 0:39














  • 1




    You never call the average function.
    – Matt
    Nov 11 at 0:39








1




1




You never call the average function.
– Matt
Nov 11 at 0:39




You never call the average function.
– Matt
Nov 11 at 0:39












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










The first problem is that you never call the average() method:



Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
System.out.println(average(numbers));


For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:



public static void lowerNums(int average, int arr) {
//For each int in the array
for(int num : arr) {
//If the int is less than the average, print it out
if(num < average) {
System.out.println(num);
}
}
}


Or using Streams:



Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);





share|improve this answer




























    up vote
    -1
    down vote













    Here's how to make an array a parameter in a method:



    public static void doStuff(int numbers){
    //super awesome code
    }


    If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.



    Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:



    public static int getSmallValues(int average, int numbers){
    Arraylist<Integer> smallNumbers = new Arraylist<>();

    for(int i: numbers){
    if(i < average){
    smallNumbers.add(i);
    }
    }

    return ((int) smallNumbers.toArray());
    }


    I hope this helps.. Merry coding!






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244724%2funable-to-pass-an-array-to-a-second-method%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      The first problem is that you never call the average() method:



      Random random= new Random();
      int numbers= new int[10];
      for (int i = 0; i < 10; i++)
      {
      int number= random.nextInt(100);
      System.out.println(number);
      numbers[i]=number;
      }
      System.out.println(average(numbers));


      For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:



      public static void lowerNums(int average, int arr) {
      //For each int in the array
      for(int num : arr) {
      //If the int is less than the average, print it out
      if(num < average) {
      System.out.println(num);
      }
      }
      }


      Or using Streams:



      Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);





      share|improve this answer

























        up vote
        0
        down vote



        accepted










        The first problem is that you never call the average() method:



        Random random= new Random();
        int numbers= new int[10];
        for (int i = 0; i < 10; i++)
        {
        int number= random.nextInt(100);
        System.out.println(number);
        numbers[i]=number;
        }
        System.out.println(average(numbers));


        For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:



        public static void lowerNums(int average, int arr) {
        //For each int in the array
        for(int num : arr) {
        //If the int is less than the average, print it out
        if(num < average) {
        System.out.println(num);
        }
        }
        }


        Or using Streams:



        Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);





        share|improve this answer























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          The first problem is that you never call the average() method:



          Random random= new Random();
          int numbers= new int[10];
          for (int i = 0; i < 10; i++)
          {
          int number= random.nextInt(100);
          System.out.println(number);
          numbers[i]=number;
          }
          System.out.println(average(numbers));


          For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:



          public static void lowerNums(int average, int arr) {
          //For each int in the array
          for(int num : arr) {
          //If the int is less than the average, print it out
          if(num < average) {
          System.out.println(num);
          }
          }
          }


          Or using Streams:



          Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);





          share|improve this answer












          The first problem is that you never call the average() method:



          Random random= new Random();
          int numbers= new int[10];
          for (int i = 0; i < 10; i++)
          {
          int number= random.nextInt(100);
          System.out.println(number);
          numbers[i]=number;
          }
          System.out.println(average(numbers));


          For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:



          public static void lowerNums(int average, int arr) {
          //For each int in the array
          for(int num : arr) {
          //If the int is less than the average, print it out
          if(num < average) {
          System.out.println(num);
          }
          }
          }


          Or using Streams:



          Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 3:01









          GBlodgett

          7,14541329




          7,14541329
























              up vote
              -1
              down vote













              Here's how to make an array a parameter in a method:



              public static void doStuff(int numbers){
              //super awesome code
              }


              If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.



              Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:



              public static int getSmallValues(int average, int numbers){
              Arraylist<Integer> smallNumbers = new Arraylist<>();

              for(int i: numbers){
              if(i < average){
              smallNumbers.add(i);
              }
              }

              return ((int) smallNumbers.toArray());
              }


              I hope this helps.. Merry coding!






              share|improve this answer

























                up vote
                -1
                down vote













                Here's how to make an array a parameter in a method:



                public static void doStuff(int numbers){
                //super awesome code
                }


                If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.



                Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:



                public static int getSmallValues(int average, int numbers){
                Arraylist<Integer> smallNumbers = new Arraylist<>();

                for(int i: numbers){
                if(i < average){
                smallNumbers.add(i);
                }
                }

                return ((int) smallNumbers.toArray());
                }


                I hope this helps.. Merry coding!






                share|improve this answer























                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  Here's how to make an array a parameter in a method:



                  public static void doStuff(int numbers){
                  //super awesome code
                  }


                  If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.



                  Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:



                  public static int getSmallValues(int average, int numbers){
                  Arraylist<Integer> smallNumbers = new Arraylist<>();

                  for(int i: numbers){
                  if(i < average){
                  smallNumbers.add(i);
                  }
                  }

                  return ((int) smallNumbers.toArray());
                  }


                  I hope this helps.. Merry coding!






                  share|improve this answer












                  Here's how to make an array a parameter in a method:



                  public static void doStuff(int numbers){
                  //super awesome code
                  }


                  If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.



                  Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:



                  public static int getSmallValues(int average, int numbers){
                  Arraylist<Integer> smallNumbers = new Arraylist<>();

                  for(int i: numbers){
                  if(i < average){
                  smallNumbers.add(i);
                  }
                  }

                  return ((int) smallNumbers.toArray());
                  }


                  I hope this helps.. Merry coding!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 0:49









                  Taslim

                  1,11741124




                  1,11741124






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244724%2funable-to-pass-an-array-to-a-second-method%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly