0

2 つの必須コマンド ライン引数と 1 つのオプション コマンド ライン引数を使用する Java プログラミングの課題があります。私の課題の詳細、私のプログラムが実行すべきもののサンプル出力、およびこれまでにコーディングしたものを皆さんにお伝えしましょう。

  • コマンド ライン引数 1 は、テキスト ファイルの名前を指定します。
  • オプションのコマンド ライン引数 -i を使用する場合は、最初の引数の後、2 番目の必須パラメーターの前に指定する必要があります。これは、検索で大文字と小文字が区別されないことを示します。

  • 2 番目の必須パラメーター/コマンド ライン引数は、プログラムがファイル内で検索する文字列 (1 文字以上の長さ) であり、最初の必須コマンド ライン引数で指定されています。

サンプル出力:

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

プログラムを実行すると、出力に使用方法が表示されるため、プログラムが正しくないと確信しています。指定された文字列の出現回数を出力するための 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 に答える