*
The moose likes Java in General and the fly likes To count the number of lines present under conditional statements(if, if, else if, else) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "To count the number of lines present under conditional statements(if, if, else if, else)" Watch "To count the number of lines present under conditional statements(if, if, else if, else)" New topic
Author

To count the number of lines present under conditional statements(if, if, else if, else)

Sandeep Koney
Greenhorn

Joined: Dec 02, 2010
Posts: 6
Hi All,

I need some help in java. Can someone help me how to count the number of statement present under conditional statements (if, if, else if, else) from a java file with the help of another java file.

To explain more better, I have a java file which includes If conditions, else if conditions, While loops also. I am writing an utility to read the java file and i want to count the number of lines present under conditional statements(if, if, else if, else).

Can someone throw some light on this issue.

Thanks & Regards,
Sandeep
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

First you have to write a parser for the Java language. Then you have to take the results of that parse, which will probably be some kind of tree structure in which (for example) an if-statement is represented by a particular kind of node (the "if") which has an attribute (which must be a boolean expression) and a list of children which are statements.

I'm assuming that when you say "the number of statement present under" an if-statement you mean the number of statements in the block controlled by the if-statement for that description. The answer then would be simply the number of children of that node. (I wouldn't have used the word "under" in that case but at any rate if you meant something different, then you would have to do something different in your code.)

What you say later suggests that you think that you can also count the number of lines. Consider that you can have several statements on one line, and conversely that one statement can extend over more than one line. For example:


So you have to clean up your requirements a bit to decide whether you want to count statements or lines. If you want to count lines, your parse tree will have to keep track of what is on what line.
Sandeep Koney
Greenhorn

Joined: Dec 02, 2010
Posts: 6
Hi Paul,

Thanks for the reply. Yes, i am talking about the number of statements in the block controlled by the if-statement for that description.
How can we acheive that.. is there any sample code for this kind of requirement.

Thanks & Regards,
Sandeep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To count the number of lines present under conditional statements(if, if, else if, else)