简体   繁体   中英

Java: Style-Correcting Code: finding comment

I'm writing these algorithms for a style-correcting program which makes adjustments to a java source file, that can compile without error. It is currently set to read the file line-by-line.

Right now, I'm having trouble writing 2 methods/algorithms which

  1. Determine if the current line (string) has comments, and
  2. finds where the comment starts

I currently have:

 public static int FindComment (String TextLine) {
              int EndOfCode = TextLine.lastIndexOf(";  ");
              return TextLine.indexOf("//", EndOfCode);

           }

        public static boolean HasComment (String TextLine) {
           if (TextLine.contains("//")) 
           {
              return true;
           }

           else
           {
              return false;
           }
        }

I know this is incorrect because I can have code, and comments, which contain " ; // ; //" as well as comments. I tried other conditional statements without success.

您好,您没有解释什么问题,但是我认为使用indexOf函数时应使用反斜杠字符进行转义。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM