title: How Do I Convert Test Scores into an array?
body: <p>I am tasked with creating a program that reads a .txt file of numbers, determines the size of the array, reopens the .txt file, and fills the array with the file's contents.  I'm looking for a solution that only uses "import java.util.* ; import java.io.* ;" to read the file and build the array with no other types of imports.  That is to say, keep it simple for me.  I don't know enough Java to make use of anything fancy.  I'm not supposed to use a fixed size for the array.  The size has to be determined by the number of numbers in the file.</p>
  
  <p>I may be able to use an Arraylist somehow, but I don't have the skill to guarantee success.  Most of the issue here is that I have very little skill.  That's why I need the solution to be as dumbed down as possible.</p>
  
  <p>The full details of the assignment:</p>
  
  <p>A local high school is giving a standardized test to all of its students.  There are 50 questions on the test and the school would like you to gather some statistics on how the students performed.  Scores can range anywhere from 0 to 50.  All the scores are stored in a file (scores.txt) that you will need to download.  </p>
  
  <p>Input the data from the file and store the data into an array.  To do this, you will have to have your program open the data file, count the number of elements in the file, close the file, initialize your array, reopen the file and load the data into the array.</p>
  
  <p>Then calculate and output the following statistics: average score, the mode of the scores, and the highest/lowest score.  In addition, output a list of the scores and the number of students that received each score.   (Hint:  you may need to use additional arrays to store information.)  </p>
  
  <p>Your program should be written using methods and should be well documented internally and externally.  Your output should be displayed using Message boxes.</p>
  
  <p>The contents of my main method so far:</p>
  
  <pre><code>  int size, index = 0;
    int[] testscores;
    String input, output;
  
    input = JOptionPane.showInputDialog("Insert filename including extension.");
  
      File scores = new File(input);
      Scanner data = new Scanner(scores);
  
      while (data.hasNext() &amp;&amp; index&lt;testscores.length)
      {
          testscores[index] = data.nextInt();
          index++;
      }
  </code></pre>
  
  <p>I don't want to use arraylists as that adds a whole level of complexity I don't have the know-how to deal with.  I need to know how I am to construct the array using the code I have setup here.  I assumed the while loop was supposed to build the array, but I need to initialize the array before hand which makes no sense.  I have two variables that I intend to make use of later on if need be.  Size can be used if I need to add something else to the main method to create a concrete size from reading the file (which is what I thought the loop would do).  I swear the class I'm taking doesn't give me the resources to do this stuff.  How hard is it to use a single line of code to get Java to do something so simple?  There should be a simple scanner method to accomplish all of this at once.</p>
  
  <p>Here is where I am at now:</p>
  
  <pre><code>  int size = 0, index = 0;
    int[] testscores = new int[size];
    String input, output;
  
    input = JOptionPane.showInputDialog("Insert filename including extension.");
  
      File scores = new File(input);
      Scanner data = new Scanner(scores);
  
      while (data.hasNext())
      {
          size++;
      }
  
    while (data.hasNext())
      {
          testscores[index]   = data.nextInt();
       JOptionPane.showMessageDialog(null, testscores[index]);
          index++;
      }
    data.close();
  </code></pre>
  
  <p>This should return a lot of windows with an array entry in each, but the program hangs.  I was hoping this code would function so I could determine that the loops work, but even if they did work, my program wouldn't be doable without editing the second loop all over again anyway.</p>