title: median and mode displaying -1
body: <p>Hey guys I have been learning coding recently and got an assignment of finding the mean, median and mode of a number of integers in an integer array. The issue that I am facing is that my median and mode displays -1 and I'm not too sure how to fix it, my &quot;if user inputs no, print an error statement&quot; is also not working and I would be very grateful if someone could help me out.
  This is my code:</p>
  <pre><code>package com.company;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  
  import static com.company.ProjectConstants.*;
  
  import java.util.*;
  
  public class Main {
  
  
      public static void main(String[] args) {
  
          int[] a = new int[MAXDATA];
          int counter = 0;
          boolean fileDone = false;
          boolean inputOk;
          String userInput;
          String theDataFile;
  
          Scanner s = new Scanner(System.in);
  
          genProgInfo();
  
          userInput = s.nextLine();
          userInput = userInput.toLowerCase();
  
          while (!userInput.equals(&quot;yes&quot;) &amp;&amp; (!userInput.equals(&quot;no&quot;))) {
              System.out.println(&quot;ERROR: Please input either yes or no: &quot;);
              userInput = s.nextLine();
              userInput = userInput.toLowerCase();
          }
  
          inputOk = userInput.equals(&quot;yes&quot;);
  
          initDataStorage(a);
  
  
          //do {
          try {
              // create file &amp; scanner objects
              System.out.println(&quot;enter one of the file names:\nData10File.txt\nData30file.txt\nData35file.txt&quot;);
              theDataFile = s.next();
              theDataFile = theDataFile.toLowerCase();
              //fName = userInput;
              File f = new File(theDataFile);
              Scanner sc = new Scanner(f);
  
              // store file data in array, a
              for (int i = 0; i &lt; MAXDATA; i++) {
  
                  if (sc.hasNext()) {
                      a[i] = sc.nextInt();
  
                  } else {
  
                      fileDone = true;
                      sc.close();
                      break;
                  }
  
              }
  
              // print error message if file data exceeds the range of array
              if (!fileDone) {
                  System.out.println(&quot;\n\tCAUTION: file has additional data, consider making array larger.&quot;);
              }
  
  
          } catch (FileNotFoundException e) {
              System.out.println(e);
              e.printStackTrace();
          }
          //} while (inputOk);
  
  
          s.close();
  
          for (int i=0; i&lt;MAXDATA; i++) {
              if (a[i] != -1) {
                  counter = a[i];
              }
          }
  
          System.out.println(&quot;counter: &quot;+ counter);
  
          displayResults(calcMean(a), calcMedian(a), calcMode(a));
  
  
      }
  
  
      public static void initDataStorage(int[] data) {
  
          for (int i = 0; i &lt; MAXDATA; i++) {
              data[i] = INVALID;
          }
  
      }
  
  
      public static double calcMean(int[] data) {
  
  
          int counter = 0;
          int mean;
          int sum = 0;
  
          for (int i = 0; i &lt; MAXDATA; i++) {
              if (data[i] != -1) {
                  sum += data[i];
                  counter++;
              }
          }
  
  
          mean = sum / counter;
          return mean;
      }
  
  
      public static double calcMedian(int[] data) {
  
          int middle = data.length / 2;
          if (data.length % 2 == 1) {
              return data[middle];
          } else {
              return (data[middle -1] + data[middle]) / 2.0;
          }
      }
  
      public static int calcMode(int[] data) {
          int mode = 0, maxCount = 0;
  
          for (int i = 0; i &lt; data.length; ++i) {
              int count = 0;
              for (int j = 0; j &lt; data.length; ++j) {
                  if (data[j] == data[i]) {
                      count++;
                  }
              }
  
              if (count &gt; maxCount) {
                  maxCount = count;
                  mode = data[i];
  
              }
          }
  
  
          return mode;
      }
  
  
      public static void genProgInfo() {
  
          System.out.println(&quot;This program will calculate the mean, median, and mode of a number of integers stored in the array&quot;);
          System.out.println(&quot;Would you like to continue?&quot;);
  
      }
  
  
      public static void displayResults(double mean, double median, int mode) {
  
          System.out.println(&quot;mean: &quot; + mean);
          System.out.println(&quot;median: &quot; + median);
          System.out.println(&quot;mode: &quot; + mode);
      }
  
  
  }
  </code></pre>