Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:12:12+00:00 2026-05-29T06:12:12+00:00

Writing a lexer of .java source files in Java. I have a stream of

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:12:13+00:00Added an answer on May 29, 2026 at 6:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a file utility to strip out all non-ASCII characters from files. I have
I'm trying to make a meta-language for writing markup code (such as xml and
I am writing a parser for delphi's dfm's files. The lexer looks like this:
I'm writing a lexer in haskell. Here's the code: lexer :: String -> [Token]
I'm writing a custom flex file to generate a lexer for use with JSyntaxpane.
I'm writing BNF for JavaScript which will be used to generate a lexer and
Writing my first Linq application, and I'm trying to find the best way to
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
Writing documentation in html requires some code examples. What to do with characters that
I'm writing a lexer in Prolog which will be used as a part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.