【问题标题】:Reading a text file using the BufferedReader class in Java [closed]使用 Java 中的 BufferedReader 类读取文本文件 [关闭]
【发布时间】:2013-01-05 17:45:27
【问题描述】:

我正在尝试创建一个名为 findOccurrences() 的静态方法,它接受一个名为 subString 的单个 String 参数。首先,该方法必须提示用户输入带有.txt 扩展名的文件的路径名,然后使用该文件创建File 对象。然后,该方法需要在该文件上打开一个流。随后,该方法必须一次从流中读取一行,并且对于每一行,该方法需要将字符串参数subString 在该行中的出现次数打印到标准输出。

该方法的示例输出应如下所示:

The substring "FRED" occurs 2 times in line 1
The substring "FRED" occurs 1 times in line 2
The substring "FRED" occurs 4 times in line 3
The substring "FRED" occurs 0 times in line 4

该方法需要确保当while 循环终止时,文件中的所有行都已被读取。保存文件当前行的字符串应转换为大写。在另一个循环中,该方法需要确定其参数(转换为大写)作为当前行中的子字符串出现的次数。因此,例如,如果当前行包含字符串“BRITAIN POSSESSES TALENT”,搜索子字符串“SSES”应该会出现两次。

现在,我尽了最大努力,并为所需方法开发了以下代码。但不幸的是,每次我编译它时,编译器都会抛出一个错误:foreach not applicable to expression type,这就是我卡住的地方!所以我只是想知道你们是否可以帮忙找出我哪里出错了。

下面是我开发的全部代码:

public static void countOccurrences (String subString)
{
   OUDialog.alert("Please choose a file to search");
   String pathname = OUFileChooser.getFilename();
   File aFile = new File(pathname);
   BufferedReader bufferedFileReader = null;

   int numOfOccurrencesInLine;
   int lineNumber = 0;
   int lineIndex;
   subString = subString.toUpperCase();
   String currentLine;
   try
   {
      bufferedFileReader = new BufferedReader(new FileReader(aFile));
      currentLine = bufferedFileReader.readLine();
      while (currentLine != null)
      {
         currentLine = bufferedFileReader.readLine();
         currentLine.toUpperCase();
         lineIndex = currentLine.indexOf(subString, lineNumber);
         subString = currentLine.substring(lineIndex);
         numOfOccurrencesInLine = 0;
         for (int eachOccurrence : currentLine)
         {
            numOfOccurrencesInLine = numOfOccurrencesInLine + eachOccurrence;
            System.out.println("The substring " + subString + "occurs 2 times in line " + lineNumber);
         }
      }
   }
   catch (Exception anException)
   {
      System.out.println("Error: " + anException);
   }
   finally
   {
      try
      {
         bufferedFileReader.close();
      }
      catch (Exception anException)
      {
         System.out.println("Error: " + anException);
      }
   }
} 

谢谢你们。

【问题讨论】:

  • 就个人而言,我认为这对于单一方法来说功能太多了。但是 currentLine 是一个字符串;那时你真正想做什么?
  • currentLine 是一个字符串,不是 int 的可迭代对象
  • 阅读docs.oracle.com/javase/tutorial/essential/io/charstreams.html的最后一段。你读行的方式也是错误的。并阅读 String.indexOf() 的 javadoc:将行号作为参数传递没有意义。
  • 谢谢你们,感谢你们的cmets,我会继续努力的。

标签: java substring bufferedreader


【解决方案1】:

除了您确实应该将这些功能拆分为单个方法这一事实之外,您还使用了foreach 错误。您正在遍历 String[] 数组,因此您需要将“int eachOccurrence”更改为 String eachOccurrence。
编辑:您实际上并没有遍历数组。您想遍历 String,但 String 没有实现 Iterable。
你真的应该拿一本像样的编程书。再次仔细阅读您的代码后,有几个错误。例如。你读了你的行两次,覆盖了第一个读行。因此,只需留下第二个currentLine = bufferedFileReader.readLine();(nullcheck 之后的那个)。

【讨论】:

    猜你喜欢
    • 2013-04-12
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    相关资源
    最近更新 更多