Java 源文件中的新行:如何使用 Character 类测试它们?

发布于 2025-01-02 06:38:19 字数 928 浏览 2 评论 0原文

用 Java 编写 .java 源文件的词法分析器。我有一个字符流,我试图让词法分析器跳过单行注释。

我循环遍历每个字符,我的假设是应该可以首先检测注释的 // ,然后跳过后续字符直到下一个新行字符。但它无法工作,我无法检测到任何新行字符。这是我的代码:

//is it a single line comment?
if(currentChar == '/') {

    //loop through char:s until next new line
    while(inComment == true) {
        //increment loop
        i++;

        //extract next char
        currentChar = stringInput.charAt(i);

        //check if current character is a new line
        if(( currentChar == '\n' ) || ( currentChar == '\r' )) {
            inComment = false;
            System.out.println("End Of Line Comment.");                             
        }
    }
}

那么,.java 源文件是否有换行符?是否可以使用 Character 类或任何其他方式检测它们?

非常感谢!

解决方案:

使用 BufferedReader 从 .java 源文件读取代码并将行附加到 StringBuilder 时,新行字符似乎丢失了。通过使用 org.apache.commons.io.FileUtils 中的 readFileToString() 读取 .java 文件解决了这个问题,这很有魅力!

Writing a lexer of .java source files in Java. I have a stream of characters and I trying to make the lexer skip single-line comments.

I loop through each char and my hypothesis is that it should be possible to first detect the // of the comment and then skip subsequent chars until the next new line character. But it cannot work and I cannot detect any new line character. This is my code:

//is it a single line comment?
if(currentChar == '/') {

    //loop through char:s until next new line
    while(inComment == true) {
        //increment loop
        i++;

        //extract next char
        currentChar = stringInput.charAt(i);

        //check if current character is a new line
        if(( currentChar == '\n' ) || ( currentChar == '\r' )) {
            inComment = false;
            System.out.println("End Of Line Comment.");                             
        }
    }
}

So, does .java source files have new line characters? Is it possible to detect them using the Character class or in any other way?

Many thanks in advance!

SOLUTION:

The new line characters seem to been lost while reading the code from the .java source file using a BufferedReader and appending the lines to a StringBuilder. The problem was solved by instead reading the .java file using readFileToString() from org.apache.commons.io.FileUtils which worked a charm!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

双手揣兜 2025-01-09 06:38:19

你如何读取stringInput?如果您使用 readLine,为什么不直接遵循这个伪代码:

if (stringInput starts with "//")
    readNextLine()

更短且更容易遵循。 提示:通读 String API。

How do you read stringInput? If you're using readLine, why not just follow this psuedo-code:

if (stringInput starts with "//")
    readNextLine()

Much shorter and easier to follow. Hint: Read through the String API.

苏佲洛 2025-01-09 06:38:19

这应该有效,比较 currentChar == '\n' 应该可以正常工作,并在到达行尾时返回 true。

您确定在读取文件时(例如使用 BufferedReader.readLine())时您的换行符不会丢失吗?如果是这种情况,请尝试另一种方法将文件读入字符串,例如使用 jakarta commons-io 中的 FileUtils.readFileToString。

That should work, the comparison currentChar == '\n' should work fine and return true when you reached the end of the line.

Are you sure that your line breaks don't get lost already when reading in the file, e.g. by using BufferedReader.readLine()? If that could be the case, try another way to read the file into a String, e.g. use FileUtils.readFileToString from the jakarta commons-io.

倾听心声的旋律 2025-01-09 06:38:19

在 if 条件和 while 循环之间,您是否将 inComment 值设置为 true ?您还必须检查是否有两个斜杠。

Between your if condition and while loop are you setting the inComment value to true ? Also you have to check for two slashes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文