rasheed
Compile error
2 Answer(s)      4 years and 9 months ago
Posted in : Java Beginners

View Answers

September 19, 2008 at 10:14 AM


Hi friend,

Do some changes in the code then compile Successfully.

"throws Exception" add after main(String[] args)

public static void main(String[] args) throws Exception

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class TestLineCounter
{
public static long totalLines;
public static long classDeclarations;
public static int classCount;
public static int methodCount;

public TestLineCounter()
{
}

/*
* Determines if the string is a comment line. All Comment line start with a // or a '*'
*/
public boolean isComment(String str)
{
if (str.startsWith("//"))
{
return true;
}
else if (str.startsWith("*"))
{
return true;
}
return false;
}

/**
* Check to see if the string is a class declaration. Class declarations begin with a modifier public, private protected or no modifier
*/
public boolean isClass(String str)
{
String[] splitStr = str.split("\\s"); // Split the string on a space character

if (splitStr.length > 0)
{
if ( splitStr[0].equalsIgnoreCase("class") )
{
return true;
}

if ( (splitStr[0].equalsIgnoreCase("public")) ||
(splitStr[0].equalsIgnoreCase("private")) ||
(splitStr[0].equalsIgnoreCase("protected")) )
{

if ( splitStr[0].equalsIgnoreCase("class") )
{
return true;
}
}
}

return true;
}

/**
* Determines if the string is a method declaration. There are several ways to declare a method.
* A method usually has a modifier (public, private, protected) and/or several others. It might not
* even have a modifier so looking for this would not be good. So the best thing to look for would be a open parenthesis '(', then a close parenthesis ')',
*then a open bracket '{' should follow the last parenthesis. Based on my coding standards this will work since i put the bracket on the same line.
*/


public boolean isMethod(String str)
{
if (str.startsWith("("))
{
return true;
}
else if (str.endsWith(")"))
{
return true;
}
else if (str.endsWith("}"))
{
return true;
}

return false;
}

/**
* Checks to see if the line is just whitespace. White space could be the newline
* character '\n', space character '\s', etc. Trim should remove all whitespace from a string
*/
public boolean isWhitespace(String str)
{
String tempString = str.trim();
if (tempString.length() > 0)
{
// an open bracket '{', usually goes on the end of line in my code so I don't count this
// you have to determine if you want to count it
if (tempString.equalsIgnoreCase("{"))
{
return false;
}
else
{
return true;
}
}
return false;
}

public void countLines(BufferedReader inFile) throws Exception
{
String str;

int partCount = 0;

while ((str = inFile.readLine()) != null)
{
if (isComment(str))
{
return;// Don't count. YOu could keep track if you want too
}
else if (isClass(str))
{
classCount++;
}
else if (isMethod(str))
{
totalLines++;
methodCount++;
partCount++;
}
else if (isWhitespace(str))
{
// skip
}
else
{
totalLines++;
partCount++;
}
}
}

September 19, 2008 at 10:17 AM


public static void main(String[] args) throws Exception
{
totalLines = 0;
classCount = 0;
methodCount = 0;

try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the name of the file:");
String fileName = in.readLine();

File file = new File(fileName);
BufferedReader inFile = new BufferedReader( new FileReader(file));

TestLineCounter tc = new TestLineCounter();

tc.countLines(inFile);

// Print whatever from tc
}
catch (IOException ioe)
{
System.out.println("Error : " + ioe.getMessage());
}

}
}

Thanks









Related Pages:
compile error
program with Test.java and try to compile with javac test.java an error like...compile error  Hello All for example public class A { static void main(String args[]) { System.out.println("Hello World
Compile error - WebSevices
Compile error  Hello, How to read .pdf file using Javascript/php. Any one send the code
Compile time error
Compile time error  Hi, When i compile my simple program in cmd am getting a error like "Javac is not a recognized as an internal or external command, operable program or batch file" How to resolve this problem ????  
compile time error
compile time error  my program compile successfully and make the class file also but when i tried to do next command to see the output,it showing error that "could not find or load main class fibbo" fibbo is my class name
compile time error
compile time error  my program compile successfully and make the class file also but when i tried to do next command to see the output,it showing error that "could not find or load main class fibbo" fibbo is my class name
compile time error
compile time error  my program compile successfully and make the class file also but when i tried to do next command to see the output,it showing error that "could not find or load main class fibbo" fibbo is my class name
Compile error - Java Beginners
Compile error  I get this error when compiling my program: java:167... to be thrown tc.countLines(inFile); ^ 1 error.... Thanks  Hi friend, Do some changes in the code then compile Successfully
Java compile error - Java Beginners
Java compile error  I am having trouble compiling the Java project available on this site at http://www.roseindia.net/chatserver/index.shtml... to compile Java on Mac besides using the Terminal
Java compile time error. - Java Beginners
Java compile time error.  CreateProcess error=2, The system cannot find the file specified  Pleae Describe your query  Hi friend, Please specify in detail and send me code. If you are new java
i got an error while compile this program manually.
i got an error while compile this program manually.  import javax.servlet.http.*; import org.apache.struts.action.*; public class ActionOne{ public...,struts jar files and i got an error in saveErrors() error Heading cannot find
i cant find any compile time error but there is runtime error.
i cant find any compile time error but there is runtime error.  ...){ System.out.println("Error:connection not created"); }   Hi...("Error is:" + s.getMessage()); } } catch (Exception e
Unable to compile class for JSP - WebSevices
Unable to compile class for JSP  org.apache.jasper.JasperException: Unable to compile class for JSP When I am trying to access a java file kept... getting above error. Java code in src folder is accepting webservice output
plaz help me i try to compile from last 2 day .
plaz help me i try to compile from last 2 day .   org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line...: psUpdateRecord.setString(4,Mobileno); An error occurred at line: 37 in the jsp file
plaz help me i try to compile from last 2 day .
plaz help me i try to compile from last 2 day .   org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line...: psUpdateRecord.setString(4,Mobileno); An error occurred at line: 37 in the jsp file
Java error
Java error  class WhileLoopMethod{ public static void main(String args[]){ new Hello(); } } class Hello{ int a = 5, b = 88; while(a } //it is giving compile time error .....illegal start
Java error
Java error  class WhileLoopMethod{ public static void main(String args[]){ new Hello(); } } class Hello{ int a = 5, b = 88; while(a } //it is giving compile time error .....illegal start
Java error code
Java error code       Java Error code are the set of error that occurs during the compile-time and Run-time. From java error we have given  you a sample of code
Error in JfreeChart
Error in JfreeChart  I am getting an error when i am using jfreechart for graph generation, that is: D:\my\linechart.java:5: package org.jfree.chart... the compiler and compile the code. Thanks
compilation error
compilation error  Hi my program below is not compiling,when I try to compile it i'm getting the error message "tool completed with exit code 1".The loop has to be at the end of the program can you help me. Blockquoteimport
How to compile and run the JSP
How to compile and run the JSP  Hi Kindly tell me how to run and compile JSP small Program using Tomcat as webserver. I created small program HELLO JSP in HTML and compile the program but it is saying Class o r interface
compile and run java program
compile and run java program  Hello, everyone!!! I just want to ask if you anyone here knows how to compile java file in linux specifically UBUNTU... do I compile and run this file using the terminal of ubuntu... By the way, I am
Java compilation error - Java Beginners
Java compilation error  Method overridden problems in compile time error
complier error - Java Beginners
Thanks  Hi Friend, Difference: Compile time error shows syntax... compile time and run time.  Hi Friend, Difference: Compile time error shows syntax errors like: required : int found : byte Runtime error
how to compile programs??????????
how to compile programs??????????  "javac" is not recognised as a file name. why??????????   Have you set your path name and class name correctly? Anyways have a look at the following link: Install Java
compile a java class
compile a java class  Hi, Am having a class ExampleOne.java in the location C:\learn\ExampleOne.java I want the class file to be present in C:\. I don't want to add any package details in the code. Is is possible to get
compile a java class
compile a java class  Hi, Am having a class ExampleOne.java in the location C:\learn\ExampleOne.java I want the class file to be present in C:\. I don't want to add any package details in the code. Is is possible to get
Unable to compile Servlet
Unable to compile Servlet  First I installed the tomcat server in C:\apache-tomcat-6.0.14 directory. Then I created a system variable CATALINA_HOME... was created by me. To compile my servlet program I did the following: C:\apache
JAVA Compile Question ?
JAVA Compile Question ?  I'm unable to compile this file and not sure why? Can someone please help? import java.nio.file.*; import java.nio.file.attribute.*; import java.nio.file.attribute.Attributes; import
Compiling Error - Java Beginners
Compiling Error  cant able to compile java servlet file in the command prompt? WHY? but i can compile normal java file give me answer to fix my problem
java error - Java Beginners
been obsoleted. To get the details of your error, compile your class with Xlint... Remove it, after compile then above lines will shown on command prompt. 
JFREE error again
. but then also its giving me error i am able to compile the code but now when i am...JFREE error again  hi......... As i had asked u the jfree error i... is the error which i am getting plz help to solve this error plz help
run time error
run time error  my program compile successfully but does not showing... you the following command to compile the code: javac HelloWorld.java Actually... saved your class with HelloWorld. Check it out. Even then if error occurs, post
error message - Java Beginners
error message  sir, while i m trying to execute my first program(print my name)I get error message like" javac is not recognized as internal...;Hi friend, To compile and run a java program we need to install java
what is the error - Java Beginners
what is the error  What is the compile time error that i am getting... is creating the compile time error such as : - ? CANNOT FIND SYMBOL ? because you... this .correct code is this if you don?t want such type of error EG : - import java.lang.
Compiling Error - Java Beginners
Compiling Error  cant able to compile java servlet file in the command prompt? WHY? but i can compile normal java file give me answer to fix my problem.  Hi Friend, Do you have servlet-api.jar in the lib folder
compilation error - Java Beginners
compilation error  Dear sir, When i compile some pgms i am getting below error.what may be the reason? "uses or overrides a deprecated API... the compiler is referring to, then compile your class with the following: javac
xml compile time warning - XML
xml compile time warning  hi all when i compile my code the following message is shown: Note: keysearch.java uses unchecked or unsafe oper Note: Recompile with -Xlint:unchecked for details. wat do these mean??? 
how to compile and run servlet program
how to compile and run servlet program   hello sir/mam i hve installed tomcat5.5 version and also have jdk1.6.0_14 installed but not able to run it or compile i m doing it first tyme pls help me in sorting out this problem. i hve
How to compile and run Java program
How to compile and run Java program  How to compile and Java program from command prompt on my windows 7 computer? Thanks   Hi, For compiling and running the Java program from command prompt you should must have JDK
getting an error when set classpath
getting an error when set classpath  Hello Everyone! i have used hssf... for that jar file but when i compile the program it will compile correctly but when i run it, it gives an error "could not find or load main" please help me
How to compile & get output for JSP
How to compile & get output for JSP  HI My Roseindia friend has given the answer as RUN the JSP How to run the JSP Program? But what i mean is that i created a small jsp program in HTML and saved as Hello.jsp. And next i opened
compilation error - Java Beginners
compilation error  i 'm not able to compile a program : prblem is there is one package in which 2 class person & dog reside. both classes r public... in same package Dog class is being compiled bt. Person is not compiled . error
Servlet error - Java Beginners
to compile the java files separately and then place the class files in classes folder... folder of your tomcat and compile it. Compilation is necessary. Thanks  
please fix the error
please fix the error  org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 14 in the jsp file...: { ///////////@@@@the above mentioneed is error and code is as follows
compilation error - Java Beginners
compilation error  sir what is the error inthis code after... the file "Group.java" in the c: drive . 2)For compile the program... drive and compile and run the group.java file may be it will works
java compiler error
java compiler error  I am trying to compile a simple program which is as follows: public class A { public static void main(String args[]) { B b=new B(); } } Above program is in file A.java second program
error
error  while iam compiling iam getting expected error
an error - Java Beginners
an error  Hi everyone ? pls i tried to run my following codes but i detect an error after trying to compile and run them,howevre i can't find out where i made a mistake,thus pls try to find it and help me where i have gone wrong
Error-
Error-   Hello, I would like to know about XSD file. I try to print XML file but I am getting error SAXException-- says Content is not allowed in prolog. Please help me