Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修改此java命令行程序以搜索字符串并打印该字符串的出现次数?_Java - Fatal编程技术网

如何修改此java命令行程序以搜索字符串并打印该字符串的出现次数?

如何修改此java命令行程序以搜索字符串并打印该字符串的出现次数?,java,Java,我有一个Java编程任务,使用两个必需的命令行参数和一个可选的命令行参数。让我告诉你们我的任务的细节,我的程序应该运行的示例输出,以及到目前为止我编写的代码: 命令行参数1指定文本文件的名称 可选命令行参数-i(如果使用)必须在 第一个参数,在第二个必需参数之前,表示 搜索不区分大小写 第二个必需的参数/命令行参数是字符串(一个或多个) 程序将在指定的文件中搜索的字符数(更长) 在第一个必需的命令行参数中 样本输出: % java FindOccurrences myLongFile.txt

我有一个Java编程任务,使用两个必需的命令行参数和一个可选的命令行参数。让我告诉你们我的任务的细节,我的程序应该运行的示例输出,以及到目前为止我编写的代码:

  • 命令行参数1指定文本文件的名称
  • 可选命令行参数-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};
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.StringTokenizer;
/*此程序接受两个必需的命令行参数和一个
可选第三个选项,如下所述,并打印出
指定的字符串:
-命令行参数1指定文本文件的名称
-可选命令行参数-i(如果使用)必须在
第一个参数,在第二个必需参数之前,表示
搜索不区分大小写
-第二个必需的参数/命令行参数是字符串(一个或多个)
程序将在指定的文件中搜索的字符数(更长)
在第一个必需的命令行参数中
以下是该程序的预期使用方式:
java FindOccurrences myLongFile.txt-i文件名
笔记:
-命令行参数由空格分隔
-以破折号开头的命令行参数必须为小写
-以破折号开头的命令行参数,有时是可选的,
被称为“开关”
-表达式[-i]表示搜索不区分大小写
*/
公共类FindOccurrences
{ 
公共静态void main(字符串[]args)
{
WhichCase theCase=WhichCase.case不区分大小写;//不区分大小写=>不更改大小写的输出文件内容
FileReader fr=null;
int matchFound=0;
字符串搜索字符串;
如果(args.length<2 | | args.length>3)
{ 
System.out.println(“用法:findoccurrencesfilename[-i]string”);
系统退出(-1);
} 
如果(args.length==2&&args[0]。字符(0)!='-'&&args[1]。字符(0)!='-')
theCase=WhichCase.case-sensitive;
else if(args.length==3&&args[0]。等于(“-i”))
theCase=WhichCase.case不敏感;
其他的
{ 
System.out.println(“用法:findoccurrencesfilename[-i]string”);
系统退出(-1);
}   
尝试
{ 
fr=新文件读取器(args[0]);
BufferedReader fd=新的BufferedReader(fr);
StringTokenizer令牌=null;
while(true)
{ 
String line=fd.readLine();
如果(行==null)
打破
其他的
令牌=新的StringTokenizer(行);
if(theCase==WhichCase.case敏感)
{
searchString=args[1];
while(tokens.hasMoreTokens())
System.out.println(tokens.nextToken());
matchFound++;
}
if(theCase==WhichCase.case不敏感)
{
searchString=args[2];
}
System.out.print(“字符串在文件“+args[0]”中出现“+matchFound+”次“+”);
}
fd.close();
} 
捕获(ioe异常ioe)
{ 
System.out.println(“IO错误:+ioe”);
} 
}
私有枚举,其大小写为{区分大小写,不区分大小写};
}
我很确定我的程序不是很正确,因为当我运行它时,我的输出显示用法:findoccurrencesfilename[-I]string,然后终止。我知道我在try块中缺少了一些东西,无法打印出 指定的字符串。我想我需要某种计数器来打印出这些事件发生的次数
指定的字符串。谁能帮我修改一下程序吗?我试图使我的输出看起来与上面的输出类似。谢谢你的时间

只需查看每个参数:

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

如果可以使用外部库,请查看