title: Java - Displaying directory contents based on search using recursion
body: <p>My task is to write a class RFind which recursively searches the current directory and its subdirectories, attempting to match files against its single regular expression argument.</p>
  
  <p>So far the only thing I have tried is the following which a) isn't recursive and b) doesn't really seem to work. How would you go about doing this?</p>
  
  <p>NB: The sample solution given displays how you could pass regex in the command line and how it would look:</p>
  
  <p><strong>$ java RFind ".*.java"</strong></p>
  
  <p><strong>./RFind.java</strong></p>
  
  <p><strong>./src/AHTNF.java</strong></p>
  
  <p><strong>$</strong></p>
  
  <pre><code>public class RFind {
  
      public static void main(String[] args) {
  
          Scanner in = new Scanner(System.in);
  
          System.out.print("Please enter a regular expression: ");
          String regex = in.next();
  
          Pattern p = Pattern.compile(regex);
  
          File dir = new File (".");
  
          File[] files = dir.listFiles();
          ArrayList&lt;String&gt; f = new ArrayList&lt;String&gt;();
  
          for (int i = 0; i &lt; files.length; i++) {
              Matcher m = p.matcher(files[i].toString());
              if (m.matches()) {
                  f.add(files[i].toString());
              }
          }
  
          for (int i = 0; i &lt; f.size(); i++) {
              System.out.println(f.get(i));
          }
  
      }
  
  }
  </code></pre>