title: pattern.compile help java program
body: <p>I have written a program to parse a text file which contains a sample C program with <code>if</code>, <code>else</code> and <code>while</code> condition.</p>
  
  <p>I have 2 <code>ArrayList</code>s and my program will parse through the file. I'm using <code>Matcher</code> and have specified pattern <code>String</code>s in <code>Pattern.compile()</code>. I am trying to draw a control flow graph for a particular program; however, I'm just finding the nodes for now and will link them up later.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>//import static LineMatcher.ENCODING;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.LineNumberReader;
  import java.nio.charset.Charset;
  import java.nio.charset.StandardCharsets;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public final class CFG {
  
    public void findLines(String aFileName) {
      List&lt;Integer&gt; a = new ArrayList&lt;Integer&gt;();
      List&lt;Integer&gt; b = new ArrayList&lt;Integer&gt;();
      // int [] a = new int[10000];
      // int [] b = new int[10000];
      Pattern regexp = Pattern.compile("if|else|while");
      Matcher exp1 = regexp.matcher("if");
      Matcher exp2 = regexp.matcher("else");
      Matcher exp3 = regexp.matcher("while");
  
      Path path = Paths.get(aFileName);
      try (BufferedReader reader = Files.newBufferedReader(path, ENCODING);
          LineNumberReader lineReader = new LineNumberReader(reader);) {
        String line = null;
        while ((line = lineReader.readLine()) != null) {
          // exp1.reset(line); //reset the input
          int counter = 1;
          if (exp1.find()) {
            int l = lineReader.getLineNumber();
  
            b.add(l);
          }
          if (exp2.find()) {
            int l = lineReader.getLineNumber();
  
            b.add(l);
          }
          if (exp3.find()) {
            int l = lineReader.getLineNumber();
  
            b.add(l);
          } else {
            int l = lineReader.getLineNumber();
            a.add(l);
          }
        }
        // counter++;
  
        System.out.println(a);
        System.out.println(b);
      }
  
      catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  
    final static Charset ENCODING = StandardCharsets.UTF_8;
  
    public static void main(String... arguments) {
      CFG lineMatcher = new CFG();
      lineMatcher.findLines("C:Desktop\\test.txt");
    }
  }
  </code></pre>
  
  <p>What I'm trying to do here is, if my <code>String</code> is found, enter the line number in <code>ArrayList b</code>, otherwise enter the line number in <code>ArrayList a</code>. Hence, I know, which lines have <code>if</code>, <code>else</code> and <code>while</code> statements.</p>
  
  <p>I don't know if my code is incorrect or what, the input file is as below :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main()
  {
    int i=1, sum = 0;  
    if( i = 1)  {
      sum += i;
    }  else
      printf("sum = %d\n", sum);
  
    return 0;
  }
  </code></pre>
  
  <p>and the output of the program is:</p>
  
  <pre><code>run: 
  [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  [1, 1, 1]
  </code></pre>
  
  <p>PS: I'm an amateur, this program could be logically incorrect.</p>
  
  <p>Please let me know if any more information is needed.</p>
  
  <p>EDIT :</p>
  
  <p>Code that works fine for just one string search :</p>
  
  <pre><code>Pattern regexp = Pattern.compile("if");
      Matcher matcher = regexp.matcher("if");
  
  
      Path path = Paths.get(aFileName);
      try (
        BufferedReader reader = Files.newBufferedReader(path, ENCODING);
        LineNumberReader lineReader = new LineNumberReader(reader);
      ){
        String line = null;
        while ((line = lineReader.readLine()) != null) {
         matcher.reset(line); //reset the input
  
  
         if(matcher.find())
         {
  
           int a= lineReader.getLineNumber();
           System.out.println(a);
                  }
  
        }      
      }    
      catch (IOException ex){
        ex.printStackTrace();
      }
  </code></pre>
  
  <p>Above one works fine(its just a part of code, not entire program. program is same as above one) and returns the line number where <code>if</code> is found. I used same logic and added the <code>else and while</code> part.</p>