Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Can someone Test this java program for me?

Ms. CandleLadyMs. CandleLady Posts: 13Member
I wrote a program the counts the Line of Code. The program should work but whenever I type in the file it say file not found. So could you please run the program (use a file already on your computer) and tell me if the program works for you. Please HELP!

Requirements Statement:
The program count the logical lines in a program, omitting comments and blank lines. It will use the counting standard produced by report exercise R1 to keep track of the LOC. It will then display a single count for the entire program source file.

Design:
Start with assignment 1A code. Construct file input. Sum all counting points described in R1 ( ';' and '}' ). Display. End.
*/

import java.io.*;

public class LOC {
public static void main(String arguments[]) {
BufferedReader stdIn = null;
StreamTokenizer text = null;
String filename = null;
long linesOfCode = 0;
final int semicolon = 59;
final int closeBracket = 125;

// Read in file name
try {
// Create a BufferedReader so we can use readLine
stdIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter text file name:");
filename = stdIn.readLine();
}
catch (IOException ex) {
ex.printStackTrace();
return;
}

// open the file
try {
text = new StreamTokenizer(new FileReader(filename));
text.slashSlashComments(true);
text.slashStarComments(true);
}
catch (FileNotFoundException ex) {
System.out.println("File not found");
return;
}

// loop while tokenizer is not at the end of file
for (;;) {
try {
int tokenType = text.nextToken();
if (tokenType != StreamTokenizer.TT_EOF) {
if (tokenType == semicolon || tokenType == closeBracket) {
linesOfCode++;
}
}
else {
break;
}
}
catch (IOException ex) {
ex.printStackTrace();
return;
}
}
stdIn = null;
text = null;
filename = null;

System.out.println("Lines Of Code (LOC): " + linesOfCode);
}
}

Comments

Sign In or Register to comment.