This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sandeep Koney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic