title: Reading a text file with a loop JAVA
body: <p>My program reads a set of data, processes it, prints stuff, and then is supposed to continue on to the next set of data, until there is no more. My program works if I give it one set of data, but I get an error when I add a second. </p>
  
  <p>In my BlueJ window it says </p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: 15
  </code></pre>
  
  <p>The error in the terminal says:</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: 15
  at Prog4.read_votes(Prog4.java:97)
  at Prog4.main(Prog4.java:38)
  </code></pre>
  
  <p>38 is in the main method:</p>
  
  <pre><code>p4.read_votes(stdin, votesArray);
  </code></pre>
  
  <p>97 is:</p>
  
  <pre><code>votes[i] = stdin.nextInt();
  </code></pre>
  
  <p>and it's in the read_votes method:</p>
  
  <pre><code>public void read_votes(Scanner stdin, int votes[])
  {
   //for (int i = 0; i &lt; votes.length; i++)
   //   votes[i] = stdin.nextInt();  
  
   int i = 0;
  
    while (stdin.hasNextInt())
    {
       votes[i] = stdin.nextInt();
       i++;
    }
  }
  </code></pre>
  
  <p>Here is my class and main method:</p>
  
  <pre><code>public class Prog4
  {
  int mostVotes = 0;
  int mostVotesIndex = 0;
  int fewestVotes = 0;
  int fewestVotesIndex = 0;
  
  public static void main(String args[]) throws Exception
  {
    //Scanner stdin = new Scanner(System.in);
    Scanner stdin = new Scanner(new File("test.txt"));
    int num_votes, num_candidates, election_num = 1;
  
  
  
    System.out.println("Welcome to Prog4!\n");
    Prog4 p4 = new Prog4();
  
    while (stdin.hasNextLine())
    {
  
       System.out.println("Results for election " + election_num);
  
       num_candidates = stdin.nextInt();
       String[] candidateArray = new String[num_candidates];
       p4.read_candidates(stdin, candidateArray);
  
       num_votes = stdin.nextInt();
       int[] votesArray = new int[num_votes];
       p4.read_votes(stdin, votesArray);
  
  
       p4.read_votes(stdin, votesArray);
       System.out.println("Total votes: " + votesArray.length);
       p4.process_highest_vote_getter(candidateArray, votesArray);
       p4.process_lowest_vote_getter(candidateArray, votesArray);
       p4.process_winner(candidateArray, votesArray);
    }
  
  System.out.println("Done. Normal termination of Prog4.");
  }
  </code></pre>
  
  <p>Here is the data being read from the text file:</p>
  
  <pre><code>4
  Owen
  Jack
  Scott
  Robbie
  15 0 1 1 2 3 1 0 0 0 0 1 2 2 1 1
  
  2
  Erik
  Martin
  10 0 1 0 1 0 1 0 0 0 1
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I changed the read_votes method to:</p>
  
  <pre><code>public void read_votes(Scanner stdin, int votes[])
  {
    for (int i = 0; i &lt; votes.length &amp;&amp; stdin.hasNextInt(); i++)
       votes[i] = stdin.nextInt();  
  }
  </code></pre>
  
  <p>It gives the correct output for the first set of data, but I get an error when it starts doing the second set. There error reads: </p>
  
  <pre><code>java.util.InputMismatchException
  </code></pre>
  
  <p>and it's on this line in the main method:
      num_candidates = stdin.nextInt();</p>