Java中的LineCount程序.忽略计数中的一些行



我在Java中具有以下代码:

import java.io.*;
public class LineCountingProg
{
   public static void main(String args[])
   {
      FileInputStream fis = null;
      try
      {
         fis = new FileInputStream("d://MyFile.txt");
      }
      catch(FileNotFoundException e)
      {
         System.out.println("The source file does not exist. " +e );
      }          
      LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(fis));
      String nextLine = null;
      try {
      while ((nextLine = lineCounter.readLine()) != null) {
  System.out.println(nextLine);
  if ((nextLine.startsWith("/*") && nextLine.endsWith("*/"))
            || (nextLine.trim().matches("[{};]+"))) {
          //This line needs to be ignored
          ignoredLines++;
          continue;
                    }
        }
  System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
  int fcounter  = 0;
  fcounter = lineCounter.getLineNumber()-ignoredLines ;
  System.out.println("Total " +fcounter);
  } catch (Exception done) {
  done.printStackTrace();
  }}}

我想添加一块代码,以便如果行以/*开头,以 */结束,则可以忽略行计数。另外,如果一条线仅包含;}它也应忽略这些行。我该怎么做?

在您的时循环外,您需要首先创建一个变量来计算所有被忽略的行。

int ignoredLines = 0;

内部循环,您可以添加以下条件:

if ((nextLine.startsWith("/*") && nextLine.endsWith("*/"))
    || (nextLine.trim().matches("[};]+"))) {
  //This line needs to be ignored
  ignoredLines++';
  continue;
}

最后:

System.out.println("Total number of line in this file " + lineCounter.getLineNumber() - ignoredLines);

startsWithendsWithequalsString类中找到。

相关内容

  • 没有找到相关文章

最新更新