How do I modify this java command line program to search for a string and print out the number occurrences of that string?
<p>I have a Java programming assignment to use two required command line arguments and one optional command line argument. Let me give you guys the details of my assignment,sample outputs of what my program should run and what I've coded so far:</p>

<ul>
<li>Command Line argument one specifies the name of a text file </li>
<li><p>Optional Command Line argument -i, if used, must be specified after the
first argument and before the second required parameter, indicating that
the search is case insensitive </p></li>
<li><p>The second required parameter/Command Line argument is the string (one or 
more characters long) that the program will search for in the file, which was specified 
in the first required Command Line argument </p></li>
</ul>

<p>Sample outputs: </p>

<pre><code>% java FindOccurrences myLongFile.txt -i frequentString 

The string “frequentString” (ignoring case) occurs 5 times in the file myLongFile.txt 

% java FindOccurrences myLongFile.txt frequentString 

The string “frequentString” occurs 3 time in the file myLongFile.txt 

% java FindOccurrences myLongFile.txt 

usage: FindOccurrences filename [-i] string
</code></pre>

<p>Now this is the code I have thus far:</p>

<pre><code>import java.io.BufferedReader;  
import java.io.FileReader; 
import java.io.IOException; 
import java.util.StringTokenizer;
/* This program takes two required command line arguments, and an 
optional third as detailed below, and prints out the number of occurrences of 
a specified string: 

- Command Line argument one specifies the name of a text file 
- Optional Command Line argument -i, if used, must be specified after the
first argument and before the second required parameter, indicating that
the search is case insensitive 

- The second required parameter/Command Line argument is the string (one or 
more characters long) that the program will search for in the file, which was       specified 
in the first required Command Line argument

The following is how this program is expected to be used:


java FindOccurrences myLongFile.txt -i filename 

Notes: 
       - Command line arguments are separated by spaces
       - Command line arguments starting with a dash must be lower case
       - Command line arguments starting with a dash, which are sometimes optional,
         are called "switches."
       - The expression [-i] means that the search is case insensitive 

*/
public class FindOccurrences 
{ 
  public static void main(String[] args)
  {

    WhichCase theCase = WhichCase.caseInsensitive;  // caseInsensitive =&gt; output file contents w/o changing the case 

    FileReader fr = null;
    int matchFound = 0;
    String searchString;

    if (args.length &lt; 2 || args.length &gt; 3) 
    { 
      System.out.println("usage: FindOccurrences filename [-i] string");  
      System.exit(-1); 
    } 

    else if (args.length == 2 &amp;&amp; args[0].charAt(0) != '-' &amp;&amp; args[1].charAt(0) != '-')
    theCase = WhichCase.caseSensitive;
    else if (args.length == 3 &amp;&amp; args[0].equals("-i"))
    theCase = WhichCase.caseInsensitive;
    else
    { 
      System.out.println("usage: FindOccurrences filename [-i] string");  
      System.exit(-1); 
    }   
    try 
    { 

      fr = new FileReader(args[0]); 
      BufferedReader fd = new BufferedReader(fr); 
      StringTokenizer tokens = null;
      while (true) 
      { 
        String line = fd.readLine(); 
        if (line == null) 
          break; 
        else
          tokens = new StringTokenizer(line);
        if(theCase == WhichCase.caseSensitive)
        {
          searchString = args[1];
          while (tokens.hasMoreTokens()) 
            System.out.println(tokens.nextToken());
            matchFound++;
        }
        if(theCase == WhichCase.caseInsensitive)
        {
           searchString = args[2];
        }
        System.out.print(" The string occured " + matchFound + " times" + "in the file" + args[0]);
        }
       fd.close(); 
      } 
     catch (IOException ioe) 
     { 
       System.out.println("IO error: " + ioe); 
     } 
   }

  private enum WhichCase {caseSensitive, caseInsensitive};
}
</code></pre>

<p>I'm pretty sure my program is not quite right because when I run it, my output says usage: FindOccurrences filename [-i] string, then terminates. I know that I'm missing something in in try block to print out the number of occurrences of
a specified string. I believe I need some sort of counter to print out the number of occurrences of 
a specified string. Could anyone please help guide me to correct my program? I'm trying to make my output look similar to the output above. Thanks for your time!</p>
