Can someone test this program?

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

  • Hi,
    Had a really quick look at this code and I've dumped it into a file, compiled and ran it - it worked fine (found) for my file 'test.txt'. And it sucessfully (as expected) failed to find a file that wasn't there.

    So that leaves me to believe you're telling it to look for a file in the "wrong place" possibly - this is a quite common thing if you're working in an IDE. Remember to put the file in the directory where your .Class files are and not where your .Java files are (the two places are usually different when using IDE's, such as /src and /class etc.) The common mistake is to have the file with the source files.

    Beyond that I dunno, as I said the code worked fine for my file etc.

    But if this doesn't help give us a bit more info on what you've tried etc. and I'll look into it a bit more

    Cheers
    -Phill.
    Qudon Foundation: Computing, Mathematics & Creative Think Tank
    www.qudon.com






    : 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);
    : }
    : }
    :
    :
  • After the program found the file, did it tell you the line of codes?





    : Hi,
    : Had a really quick look at this code and I've dumped it into a file, compiled and ran it - it worked fine (found) for my file 'test.txt'. And it sucessfully (as expected) failed to find a file that wasn't there.
    :
    : So that leaves me to believe you're telling it to look for a file in the "wrong place" possibly - this is a quite common thing if you're working in an IDE. Remember to put the file in the directory where your .Class files are and not where your .Java files are (the two places are usually different when using IDE's, such as /src and /class etc.) The common mistake is to have the file with the source files.
    :
    : Beyond that I dunno, as I said the code worked fine for my file etc.
    :
    : But if this doesn't help give us a bit more info on what you've tried etc. and I'll look into it a bit more
    :
    : Cheers
    : -Phill.
    : Qudon Foundation: Computing, Mathematics & Creative Think Tank
    : www.qudon.com
    :
    :
    :
    :
    :
    :
    : : 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);
    : : }
    : : }
    : :
    : :
    :

  • Hi,
    The following code in a sample file 'test.txt' produced a result of "LOC: 3" from your program.

    [code]
    void main()
    {
    int a = 0;

    return 0;
    }
    [/code]

    Hope that's of some use to you, proves it's counting and seems to be doing right sort of thing, though I'd guess it's not counting the { }

    Cheers
    -Phill.
    Qudon Foundation Computing, Mathematics & Creative Think Tank
    http://www.qudon.com


    : After the program found the file, did it tell you the line of codes?
    :
    :
    :
    :
    :
    : : Hi,
    : : Had a really quick look at this code and I've dumped it into a file, compiled and ran it - it worked fine (found) for my file 'test.txt'. And it sucessfully (as expected) failed to find a file that wasn't there.
    : :
    : : So that leaves me to believe you're telling it to look for a file in the "wrong place" possibly - this is a quite common thing if you're working in an IDE. Remember to put the file in the directory where your .Class files are and not where your .Java files are (the two places are usually different when using IDE's, such as /src and /class etc.) The common mistake is to have the file with the source files.
    : :
    : : Beyond that I dunno, as I said the code worked fine for my file etc.
    : :
    : : But if this doesn't help give us a bit more info on what you've tried etc. and I'll look into it a bit more
    : :
    : : Cheers
    : : -Phill.
    : : Qudon Foundation: Computing, Mathematics & Creative Think Tank
    : : www.qudon.com
    : :
    : :
    : :
    : :
    : :
    : :
    : : : 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);
    : : : }
    : : : }
    : : :
    : : :
    : :
    :
    :
  • I have another question what this program is suppose to take SD. java and count the lines of code. Sd.java is a prgram that I posted further down on the board. The programs build on each other. Also, do I have to put SD. java in a text file so that I can get the program to read the lines of code?


    : Hi,
    : The following code in a sample file 'test.txt' produced a result of "LOC: 3" from your program.
    :
    : [code]
    : void main()
    : {
    : int a = 0;
    :
    : return 0;
    : }
    : [/code]
    :
    : Hope that's of some use to you, proves it's counting and seems to be doing right sort of thing, though I'd guess it's not counting the { }
    :
    : Cheers
    : -Phill.
    : Qudon Foundation Computing, Mathematics & Creative Think Tank
    : http://www.qudon.com
    :
    :
    : : After the program found the file, did it tell you the line of codes?
    : :
    : :
    : :
    : :
    : :
    : : : Hi,
    : : : Had a really quick look at this code and I've dumped it into a file, compiled and ran it - it worked fine (found) for my file 'test.txt'. And it sucessfully (as expected) failed to find a file that wasn't there.
    : : :
    : : : So that leaves me to believe you're telling it to look for a file in the "wrong place" possibly - this is a quite common thing if you're working in an IDE. Remember to put the file in the directory where your .Class files are and not where your .Java files are (the two places are usually different when using IDE's, such as /src and /class etc.) The common mistake is to have the file with the source files.
    : : :
    : : : Beyond that I dunno, as I said the code worked fine for my file etc.
    : : :
    : : : But if this doesn't help give us a bit more info on what you've tried etc. and I'll look into it a bit more
    : : :
    : : : Cheers
    : : : -Phill.
    : : : Qudon Foundation: Computing, Mathematics & Creative Think Tank
    : : : www.qudon.com
    : : :
    : : :
    : : :
    : : :
    : : :
    : : :
    : : : : 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);
    : : : : }
    : : : : }
    : : : :
    : : : :
    : : :
    : :
    : :
    :

  • Not quite sure if there was one question in there or two, but anyway you don't need to put the source of SD.java in a text file - simply type in the full filename (e.g. "SD.java") at the command prompt when your line counting program starts.

    Cheers
    -Phill
    [b]Qudon Foundation Computing, Mathematics & Creative Think Tank
    http://www.qudon.com[/b]

Sign In or Register to comment.

Howdy, Stranger!

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

Categories