title: Counting the number of if and else excluding the nested if-else
body: <p>Can anyone tell me how can i write a program where i have to read a c program from a text file and then count the number of if-else statements, excluding the nested if-else.
  Here in this program i have counted the number of if and else from a text file, but how do i exlude the nested if from this count?
  please help me with it.</p>
  
  <pre><code>package softwaretesting;
  
  import java.io.*;
  import java.util.Scanner;
  
  public class SoftwareTesting {
  
  public static void main(String[] args) throws IOException {
      int countIf = 0, countElse = 0;
  
      Scanner input;
      input = new Scanner(System.in);
      String fileName;
      System.out.println("Enter the path of the file from which no of if and else statements are to be counted");
  
      fileName = input.next();
  
  
      Scanner file;
      file = new Scanner(new File(fileName));
  
      int count=0;
      while (file.hasNextLine())
      {
          String line = file.nextLine();
          if (line.indexOf("if") != -1 &amp;&amp; count%2==0 )
          {   
              countIf++;
          }
  
  
          if (line.indexOf("else") != -1  )
          {
              countElse++;
          }
  
  
      }
  
  
      {
          System.out.println("No of If statements: " + countIf);
          System.out.println("No of Else statements: " + countElse);
      }
  }
  }
  </code></pre>