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 6545851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:38:10+00:00 2026-05-25T11:38:10+00:00

I wrote a program that reads a file with buffer reader and stores data

  • 0

I wrote a program that reads a file with buffer reader and stores data in a String variable. How can i modify it to make it skip single and multi-line comments?

Here is my code:

import java.util.*;
import java.io.*;

public class IfCounter 
{
    public static void main(String[] args) throws IOException
    {
        // parameter the TA will pass in
        String fileName = args[0];

        // variable to keep track of number of if's
        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");

        // read from the text file
        while (( line = reader.readLine()) != null) 
        {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }

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

        // create one last string to look for our valid if(s) in
        // with ALL whitespace removed
        String compareString = tempString.replaceAll("\\s","");

        // check for valid if(s)
        for (int i = 0; i < compareString.length(); i++)
        {
            if (compareString.charAt(i) == ';' || compareString.charAt(i) == '}' || compareString.charAt(i) == '{') // added opening "{" for nested ifs :)
            {
                i++;

                if (compareString.charAt(i) == 'i')
                {
                    i++;

                    if (compareString.charAt(i) == 'f')
                    {
                        i++;

                        if (compareString.charAt(i) == '(')
                            ifCount++;
                    } // end if
                } // end if
            } // end if

        } // end for

        // print the number of valid "if(s) with a new line after"
        System.out.println(ifCount + "\n");

    } // end main
} // end class
  • 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-25T11:38:10+00:00Added an answer on May 25, 2026 at 11:38 am

    Change this:

        while (( line = reader.readLine()) != null) {
          stringBuilder.append(line);
          stringBuilder.append(ls);
        }
    

    to this:

        boolean multiLineComment = false;
        while (( line = reader.readLine()) != null) {
          if (!isLineAMultiLineCommentStart(line)) {
            multiLineComment = true;
          }
    
          if (multiLineComment) {
            if (!isLineAMultiLineCommentEnd(line)) {
              multiLineComment = false;
            }
          }
    
          if (!isLineAComment(line) && !multiLineComment) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
          }
        }
    

    You’ll need to create a boolean methods, isLineAComment(String line), isLineAMultiLineCommentStart, and isLineAMultiLineCommentEnd but this should be easy for you to do.

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

Sidebar

Related Questions

I'm trying to write a program that reads text from external file (string string
I am trying to write a Java program that reads an input file consisting
I want to write a program in which plays an audio file that reads
I'm trying to write a basic Erlang program that reads in a file in
I have written a C++ program that reads in a file and outputs the
Just because I've never read binary files before I wrote a program that reads
I have to write a program that read from a file that contains the
I wrote a program that forks some processes with fork(). I want to kill
I wrote a program that accepts and outputs Hebrew (i.e. right-to-left) text. In lieu
I have a program that loads a file (anywhere from 10MB to 5GB) a

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.