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
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

Phone System reconfiguration 11.1.2018 to 26.1.2018 - we are on email but incoming landline not available. Temp number 0797 4 925928
Stream Tokeniser or String TOkenizer

Posted by sree (sree), 29 January 2004
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?


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:

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
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?


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 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., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho