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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:32:01+00:00 2026-06-13T11:32:01+00:00

I have a program in Java with the following code: import java.io.*; public class

  • 0

I have a program in Java with the following code:

import java.io.*;
public class LineCountingProg
{
   public static void main(String args[])
   {
      FileInputStream fis = null;
      try
      {
         fis = new FileInputStream("d://MyFile.txt");
      }
      catch(FileNotFoundException e)
      {
         System.out.println("The source file does not exist. " +e );
      }          
      LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(fis));
      String nextLine = null;
      try {
      while ((nextLine = lineCounter.readLine()) != null) {
  System.out.println(nextLine);
  if ((nextLine.startsWith("/*") && nextLine.endsWith("*/"))
            || (nextLine.trim().matches("[{};]+"))) {
          //This line needs to be ignored
          ignoredLines++;
          continue;
                    }
        }
  System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
  int fcounter  = 0;
  fcounter = lineCounter.getLineNumber()-ignoredLines ;
  System.out.println("Total " +fcounter);
  } catch (Exception done) {
  done.printStackTrace();

  }}}

I want to add a piece of code so it could ignore line counts if a line starts with /* and ends with */. Also if a line contains only ; and } it should be also ignore those lines. How can I do that?

  • 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-06-13T11:32:02+00:00Added an answer on June 13, 2026 at 11:32 am

    Outside your while loop, you need to first create a variable to count all ignored lines.

    int ignoredLines = 0;
    

    Inside your while loop, you can add the following condition:

    if ((nextLine.startsWith("/*") && nextLine.endsWith("*/"))
        || (nextLine.trim().matches("[};]+"))) {
      //This line needs to be ignored
      ignoredLines++';
      continue;
    }
    

    Finally:

    System.out.println("Total number of line in this file " + lineCounter.getLineNumber() - ignoredLines);
    

    startsWith, endsWith and equals are found in the String class.

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

Sidebar

Related Questions

import java.io.*; public class Demo{ public static void main(String[] args){ File f = new
A newbie question. I have the following piece of Java code: import acm.program.*; import
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
I have a problem with understanding the following code: import java.awt.Dimension; import java.awt.GridLayout; import
I have the following Java class: package web; import javax.servlet.*; import javax.servlet.http.*; import java.io.*;
Please have a look at the following code package normal; import java.io.*; import java.util.*;
I am using the following code for sending e-mail (gmail) using Java program. I
I have written the following very simple Java program to ask user enter a
Is it possible to somehow 'import' a new Java class into a running program
I have this import in my code: import com.sun.jmx.snmp.SnmpOidDatabaseSupport; but i get following error:

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.