News

Java Program To Calculate Lines Of Code

0


Lines of Code (LOC) is a program written in any programming language which takes up input of a program file and calculates the number of lines of code in that program. In this article, will will write an LOC program in Java. This program will take input as a .Java file, process that file and will output the number of lines, the number of blank lines, number of commented lines and actual lines of code excluding blank lines and commented lines. Different segments of the program will be presented separately first and then all these segments will be combined to form up a complete program.

Define Class Variables

First of all define these variables to obtain input from the user and later display results to the user.

FileInputStream fis = null;

BufferedInputStream bis = null;

DataInputStream dis = null;

int totalCount=0;

int blankCount=0;

int commentCount=0;

String currentLine;

Taking File Input

You need to take input of the file name and then read the file based on file name.

Input File Name

Use Scanner class to get console input from the user.

String input;

Scanner in = new Scanner(System.in);

System.out.println(“Enter file path”);

input = in.nextLine();

Reading File

Read the file based on the input file name. For this purpose, you need to create a File object and pass it to the FileInputStream object and then pass the resultant object to BufferedInputStream object. DataInputStream object is used to hold data retrieved from the file.

File file = new File(input);

fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);

dis = new DataInputStream(bis);

Implement Proram Logic

You need to retrieve line by line data, evaluate and process it and then move to the next line. The line being read is assigned to a String variable currentLine whose condition is being checked that whether this is a blank line, commented line, or actual code line. There are two types of comments in Java. One line comment which starts with ‘//’, commenting everything after it in that specific line. Following code sets the status as commented if it encounters ‘/*’ and sets this status off when encountered with ‘*/’. When status is commented value of commentCount variable is increased in every loop.

while (dis.available() != 0) {

totalCount++;

currentLine=dis.readLine();

currentLine=currentLine.trim();

if (currentLine.length()==0)

blankCount++;

if ((currentLine.length()>1) && (currentLine.charAt(0)==’/’ ) && (currentLine.charAt(1)==’*’ ))

commentStatus=true;

if ((commentStatus==true) && (currentLine.length()>0))

{

commentCount++;

if ((currentLine.length()>1) && (currentLine.charAt(currentLine.length()-2)==’*’ ) && (currentLine.charAt(currentLine.length()-1)==’/’ ))

{commentStatus=false;

}

}

else if ((currentLine.length()>1) && (currentLine.charAt(0)==’/’ ) && (currentLine.charAt(1)==’/’ ))

commentCount++;

}

Display Output

Display output to the user.

System.out.println(“Total Lines: ” + totalCount);

System.out.println(“Blank Lines: ” + blankCount);

System.out.println(“Comment Lines: ” + commentCount);

System.out.println(“Actual Code Lines: ” + (totalCount-blankCount – commentCount));

Below is the complete program code for you to ensure that you are not missing anything

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.BufferedInputStream;

import java.lang.reflect.Method;

import java.util.Scanner;

public class CodeEvaluator1 {

public static void main(String[] args) throws IOException, ClassNotFoundException {

count ();

}

public static void count() throws IOException {

String input;

Scanner in = new Scanner(System.in);

System.out.println(“Enter file path”);

input = in.nextLine();

FileInputStream fis = null;

BufferedInputStream bis = null;

DataInputStream dis = null;

int totalCount=0;

int blankCount=0;

int commentCount=0;

String currentLine;

Boolean commentStatus=false;

try {

File file = new File(input);

fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);

dis = new DataInputStream(bis);

while (dis.available() != 0) {

totalCount++;

currentLine=dis.readLine();

currentLine=currentLine.trim();

if (currentLine.length()==0)

blankCount++;

if ((currentLine.length()>1) && (currentLine.charAt(0)==’/’ ) && (currentLine.charAt(1)==’*’ ))

commentStatus=true;

if ((commentStatus==true) && (currentLine.length()>0))

{

commentCount++;

if ((currentLine.length()>1) && (currentLine.charAt(currentLine.length()-2)==’*’ ) && (currentLine.charAt(currentLine.length()-1)==’/’ ))

{commentStatus=false;

}

}

else if ((currentLine.length()>1) && (currentLine.charAt(0)==’/’ ) && (currentLine.charAt(1)==’/’ ))

commentCount++;

}

System.out.println(“Total Lines: ” + totalCount);

System.out.println(“Blank Lines: ” + blankCount);

System.out.println(“Comment Lines: ” + commentCount);

System.out.println(“Actual Code Lines: ” + (totalCount-blankCount – commentCount));

} catch (IOException e) {

System.out.println(“Sorry file coule not be found”);

//e.printStackTrace();

} finally {

try {

fis.close();

bis.close();

dis.close();

} catch (IOException ex) {

//            ex.printStackTrace();

}

}

}

}

Learn more about programming, here.

Is Your Website 'Mobile Ready'?

Previous article

Windows Media Player Control In VB.NET (4.0)

Next article

You may also like

Comments

Comments are closed.

More in News