0

我有一个 Java 编程任务,需要使用两个必需的命令行参数和一个可选的命令行参数。让我给你们我的任务的详细信息,我的程序应该运行的示例输出以及我到目前为止编码的内容:

  • 命令行参数一指定文本文件的名称
  • 可选命令行参数 -i,如果使用,必须在第一个参数之后和第二个必需参数之前指定,表示搜索不区分大小写

  • 第二个必需的参数/命令行参数是程序将在文件中搜索的字符串(一个或多个字符长),它在第一个必需的命令行参数中指定

示例输出:

% 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

现在这是我到目前为止的代码:

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};
}

我很确定我的程序不太正确,因为当我运行它时,我的输出显示用法:FindOccurrences 文件名 [-i] 字符串,然后终止。我知道我在 try 块中遗漏了一些东西来打印指定字符串的出现次数。我相信我需要某种计数器来打印指定字符串的出现次数。任何人都可以帮助指导我纠正我的程序吗?我试图让我的输出看起来类似于上面的输出。谢谢你的时间!

4

1 回答 1

1

看看每个论点:

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];
    }
    . . .
}
于 2012-10-24T22:39:01.313 回答