2011-09-13 35 views
1

我写了下面的程序来读取文件并跳过注释,它适用于单行注释,但不适用于多行注释。有谁知道为什么?我不需要担心Strings中的“//”。而且,只有Java的意见,即 “//” 和 “/ * * /”我如何跳过缓冲区阅读器的评论?

代码:

import java.io.*; 

public class IfCounter2 
{ 
    public static boolean lineAComment(String line) 
    { 
     if (line.contains("//"))  
      return true; 

     return false; 
    } 

    public static boolean multiLineCommentStart(String line) 
    { 
     if (line.contains("/*"))  
      return true; 

     return false; 
    } 

    public static boolean multiLineCommentEnd(String line) 
    { 
     if (line.contains("*/"))  
      return true; 

     return false; 
    } 

    public static void main(String[] args) throws IOException 
    { 
     String fileName = args[0]; 

     int numArgs = args.length; 

     int ifCount = 0; 

     // create a new BufferReader 
     BufferedReader reader = new BufferedReader(new FileReader(fileName)); 
     String line = null; 
     StringBuilder stringBuilder = new StringBuilder(); 
     String ls = System.getProperty("line.separator"); 

     line = reader.readLine(); 
     // read from the text file 
     boolean multiLineComment = false; 

     while ((line = reader.readLine()) != null) 
     { 
      if (!multiLineCommentStart(line)) 
      { 
      multiLineComment = true; 
      } 

      if (multiLineComment) { 
      if (!multiLineCommentEnd(line)) 
      { 
       multiLineComment = false; 
      } 
      } 

      if (!lineAComment(line) && !multiLineComment) 
      { 
      stringBuilder.append(line); 
      stringBuilder.append(ls); 
      } 
     } 


     // create a new string with stringBuilder data 
     String tempString = stringBuilder.toString(); 
     System.out.println(stringBuilder.toString()); 

    } 
} 
+0

你如何使用这些方法?除非我误解了这个问题,否则问题出现在代码中。 – Andy

+0

在主要只是下面的方法:) – josh

+0

对不起,没有看到滚动条...:/ – Andy

回答

2

你只设置multiLineComment为真时!multiLineCommentStart(line)是成立的 - 也就是说,只要行不包含/*

+0

那究竟需要改变什么呢? – josh

+0

“multiLineComment”的用意似乎是追踪多行注释的开始和结束时间。因此,思考何时将该标志激活 - 何时开始跟踪您是否在多行注释中?提示:当行不包含多行开始时不是。 –

+0

所以我应该摆脱“!”不。那么我开始跟踪它时,行开始“/ *”? – josh

0

基本上,你的代码应该是某事像这样(未经)

boolean multiLineComment = false; 

    while ((line = reader.readLine()) != null) 
    { 
     if (multiLineCommentStart(line)) 
     { 
     multiLineComment = true; 
     } 

     if (multiLineComment) { 
     if (multiLineCommentEnd(line)) 
     { 
      multiLineComment = false; 
     } 
     } 

     if (!lineAComment(line) && (multiLineComment == false)) 
     { 
     stringBuilder.append(line); 
     stringBuilder.append(ls); 
     } 
    } 
在这最后

if语句,你需要与你的可变和固定

表达
0

安迪的回答是正确的这笔钱,但需要在最后的验证,如果要确保你不计算* /作为一个有效的行:

 
    boolean multiLineComment = false; 

    while ((line = reader.readLine()) != null) 
    { 
     if (multiLineCommentStart(line)) 
     { 
     multiLineComment = true; 
     } 

     if (multiLineComment) { 
     if (multiLineCommentEnd(line)) 
     { 
      multiLineComment = false; 
     } 
     } 

     if (!lineAComment(line) && (multiLineComment == false) && 
      !multiLineCommentEnd(line)) 
     { 
     stringBuilder.append(line); 
     stringBuilder.append(ls); 
     } 
    }