简体   繁体   中英

How do I modify this java command line program to search for a string and print out the number occurrences of that string?

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:

  • 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

Sample outputs:

% 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

Now this is the code I have thus far:

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 => output file contents w/o changing the case 

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

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

    else if (args.length == 2 && args[0].charAt(0) != '-' && args[1].charAt(0) != '-')
    theCase = WhichCase.caseSensitive;
    else if (args.length == 3 && 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};
}

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!

Just look at each of the arguments:

public static void main(String[] args) {
    String fileName = args[0];
    String searchString;
    boolean caseInsensitive;
    if (args.length == 2) {
        caseInsensitive = false;
        searchString = args[1];
    } else {
        caseInsensitive = true;
        searchString = args[2];
    }
    . . .
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM