title: Index out of bounds for an array list (java)
body: <p>This is for a homework assignment. I am trying to make a program that searches a text file for specific words and then prints out the frequency of the word. </p>
  
  <pre><code>public class WordFreq extends Echo{
  
  ArrayList&lt;WordCount&gt;array1=new ArrayList&lt;WordCount&gt;();
  String[] words;
  int wordsTotal=0;
  
  
  public WordFreq(String f, String x) throws IOException
  {
    super(f);
    words=x.toLowerCase().split(" ");
  }
  
  public void processLine(String line){ 
    String[] lines=line.toLowerCase().split(" ");
    wordsTotal=wordsTotal+lines.length;
    for(int j=0; j&lt;lines.length; j++){
      WordCount alpha=new WordCount(lines[j]);
      alpha.incCount();
      array1.add(alpha);}
    for(int x=0; x&lt;array1.size(); x++){
      for(int y=0; y&lt;array1.size(); y++){
        if(array1.get(x).equals(array1.get(y))&amp;&amp;(x!=y)){
          for(int i = 0; i&lt; array1.get(y).getCount(); i++){
            array1.get(x).incCount();
          }
          array1.remove(y);
        }
      }
    }
  
  }
  
  public void reportFrequencies(){
    for(int i = 0; i&lt;array1.size();i++){
      // System.out.println(array1.get(i).getWord()+" "+array1.get(i).getCount());
    }
    int currentWord=0;
    for(int x=0; x&lt;words.length; x++){
      for(int y=0; y&lt;array1.size(); y++){
        if(words[x].equals(array1.get(y).getWord())){
          currentWord=array1.get(y).getCount();}}
      System.out.print(words[x]+" ");
      System.out.printf("%.4f",(double)currentWord/wordsTotal);
    }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is my main method: </p>
  
  <pre><code>public class FreqStudy{
    public static void main(String[] args) throws IOException
    {
      Scanner scan = new Scanner(System.in);
      System.out.println("enter file name");
      String fileName = scan.next();
      Scanner scan2 = new Scanner(System.in);
      System.out.println("enter words to search for");
      System.out.println("enter lower case, separated by spaces");
      String wordString = scan2.nextLine();
      WordFreq f = new WordFreq(fileName,wordString);
      f.readLines();
      f.reportFrequencies();
    }
  }
  </code></pre>
  
  <p>I am using a .txt file of the book Emma by Jane Austen. This is the error message that I get when I run the program and try to search for words: </p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Index: 906, Size: 906
  at java.util.ArrayList.RangeCheck(ArrayList.java:547)
  at java.util.ArrayList.get(ArrayList.java:322)
  at WordFreq.processLine(WordFreq.java:26)
  at Echo.readLines(Echo.java:16)
  at FreqStudy.main(FreqStudy.java:15)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)"
  </code></pre>
  
  <p>Here are the codes for Echo and WordCount:</p>
  
  <pre><code>public class WordCount{
  
  private String word;
  private int count;
  
  public WordCount(String w){
  word = w;
  count = 0;
  }
  
  public String getWord(){
  return word;}
  
  public int getCount(){
  return count;}
  
  public void incCount(){count++;}
  
  public String toString() {
  return(word +  " --- " + count);
  }
  
  public boolean equals(Object other){
  WordCount i = (WordCount)other;
  return (this.word.equals(i.word));
  }
  }
  </code></pre>
  
  <p>echo:</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  
  public class Echo{
  String fileName; // external file name
  Scanner scan; // Scanner object for reading from external file
  
  public Echo(String f) throws IOException
  {
  fileName = f;
  scan = new Scanner(new FileReader(fileName));
  }
  
  public void readLines(){ // reads lines, hands each to processLine
  while(scan.hasNext()){
    processLine(scan.nextLine());
  }
  scan.close();
  }
  
  public void processLine(String line){ // does the real processing work
  System.out.println(line);
  }
  }
  </code></pre>
  
  <p>Line 26 of my code is: </p>
  
  <pre><code>for(int i = 0; i&lt; array1.get(y).getCount(); i++)
  </code></pre>