title: Access an ArrayList from a method in another class
body: <p>I am trying to make a program that takes input from a file and then searches for the input in a different file, for example, it takes the word "car" from file A and outputs the location of the word(if the word is included) in file B.</p>
  
  <p>At the moment I have 3 classes. A main class, a class to read the file input and a class to search for the input in another file.</p>
  
  <p>The class that reads the input has code that takes the files, reads the file line by line and saves each word into a variable, and then adds these variables into an arrayList.</p>
  
  <p>This is part of the code:</p>
  
  <pre><code>List&lt;String&gt; listOfWords = new ArrayList&lt;String&gt;();
       while((strLine = br.readLine()) != null){
            String [] tokens = strLine.split("\\s+");
            String [] words = tokens;
            for(String word : words){
                listOfWords.add(word);
                System.out.print(word);
                System.out.print(" ");      
            } 
          System.out.print("\n");  
      }
          in.close();
          return listOfWords;    
      }
  </code></pre>
  
  <p>I then need to take the arrayList and put it into the class with the code that searches for the words in an external file.</p>
  
  <p>This is part of the code that searches for the words:</p>
  
  <pre><code>public void search(List&lt;String&gt; listOfWords) throws FileNotFoundException, IOException{
  
          FileInputStream fstream = new FileInputStream(getSearchFileName());
  
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
  
          while((strLine = br.readLine()) != null){
              for(String list: listOfWords){
              Pattern p = Pattern.compile(list);
              Matcher m = p.matcher(strLine);
  
          int start = 0;
          while (m.find(start)) {
              System.out.printf("Word found: %s at index %d to %d.%n", m.group(), m.start(), m.end());
              start = m.end();
                  }
              }
            }  
  }
  </code></pre>
  
  <p>The code works standalone, but I am having trouble calling the arrayList (listOfWords) from the first set of code and using it in the second set of code, which are in different classes.</p>
  
  <p>Any help with what code I need to add or adjust will be appreciated.</p>
  
  <p>Regards</p>