title: How to calculate the average of even and odd numbers in an array?
body: <p>As a class exercise I have to code using methods a program that:</p>
  
  <p>1) Calculates the average of even and odd numbers in an array.</p>
  
  <p>I expect on using one method to find the average of even and odd numbers. However, I'm having trouble on returning the right average. For example, if I enter only odd numbers I get an error, and vice versa.</p>
  
  <p>This error:
  "java.lang.ArithmeticException: / zero"</p>
  
  <p>Also, if it were possible I would like to get some help on coding the rest of the exercise which asks for:</p>
  
  <p>2) Print the highest and lowest number in the array
  3) Allow the user to modify any of the numbers of the array</p>
  
  <p>So far I have this code:</p>
  
  <pre><code>public static void main (String args[]){
  
      int x[] = new int[4];
      Scanner input = new Scanner(System.in);
  
      for(int i = 0; i &lt; x.length ; i++){
          System.out.println("Enter a number: ");
          x[i] = input.nextInt();
      }
  
  
      System.out.println("Average of even numbers: " + getAverage(x));
      System.out.println("Average of odd numbers: " + getAverage(x));
  
   }
  
  public static int getAverage(int a[]){
      int add_even = 0;
      int counter_even = 0;
      int average_even = 0;
  
      int add_odd = 0;
      int counter_odd = 0;
      int average_odd = 0;
  
  
      for(int i = 0; i &lt; a.length; i++){
          if(a[i] % 2 == 0){
              add_even += a[i];
              counter_even++;
          }
          else if(a[i] % 2 == 1) {
              add_odd += a[i];
              counter_odd++;
          }
  
      }
  
      if (add_even % 2 == 1 &amp;&amp; add_odd % 2 == 1){
      average_even = 0;
      average_odd = add_odd / counter_odd;
  
      return average_even;
     }
     else if (add_even % 2 == 0 &amp;&amp; add_odd % 2 == 0){
      average_even = add_even / counter_even;
      average_odd = 0;
  
      return average_even;
      }
     else{
  
      average_even = 0;
      average_odd = add_odd / counter_odd;
  
      return average_odd;
  
      }
  
  
  } 
  </code></pre>
  
  <p>Thank you!</p>