Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Stream Tokeniser or String TOkenizer

Posted by sree (sree), 29 January 2004
Hi,
I need to write a program that caculates the logical lines of code in  any program that is written using Java langauge. I have jus started  learning Java. I think I can use String Tokenizer for my program.

Basically, I have to look for the keywords in java language, ;, to count the number of lines in the program. Is String Tokenizer is alone sufficient for this program or I have to use some other function to do  this. My friends told I can try with split() and ReplaceAll()
function. But I don't know the usage of these 2 functions. So can you help by giving me small sample program. Later I can work on it and develope it on my own. What is stream tokenizer? How can I use in my project?


Thanks


Posted by admin (Graham Ellis), 29 January 2004
For a short utility program to analyse a text file that's written to a particular format (e.g. to analyse a program source file), I would probably not recommend you use Java;  Java is ideal for larger applications and systems but on shorter tasks you'll find a language such as Perl or Python is much more productive, even though you might feel that you're being unfaithful to Java by using another language  

Python solution:

Code:
import sys
info = open(sys.argv[1]).read()
sofar = 0
for char in info:
   if char == ";": sofar += 1
print "There were "+str(sofar)+" semicolons"


For comparison, I would expect the code to be 4 to 5 times longer than this in Java ....

Posted by sree (sree), 29 January 2004
Hi,
Thanks for that piece of information. But I have to do this using Java only. So, I like to know which one is more suitable Stream or String Tokenizer?

Thanks
Sree

Posted by admin (Graham Ellis), 29 January 2004
If you have to do this job (inappropriately) in Java, then I would suggest you open a stream and read byte by byte, checking for the ";" character.   There seems little point in you reading (for example) each line into a string for you to then have to start breaking that string down with a tokenizer.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho