Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi im currently trying to construct a piece of code that uses a string tokenizer to find statements within a given piece of code( ie for...while...if.....switch etc) I have neber used the sting tokenizer before and im having problems with it. The problem is that i have the program running but when looping through the program to find the statements, it reads into the comments in the code which i need to stop happening. Iv heard this can be done but cant find out how to. My code is below;
import java.io.*;
import java.util.*;class Complexity{
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter screen = new PrintWriter(System.out, true);
public static void main(String[]args)throws IOException{String line = null;
String f = "for";
String i = "if";
String s = "switch";
String w = "while";
String file;
int cc;
int count = 0;
screen.println("Please input the name of the file which you would like to be read: ");screen.flush();
file = new String(keyboard.readLine());
BufferedReader reader = new BufferedReader(new FileReader(file));line = reader.readLine();
while (line!= null){
StringTokenizer tok = new StringTokenizer(line);
while (tok.hasMoreTokens()){
String currToken = tok.nextToken();
if (currToken.equals(w)){
count++;
}
if (currToken.equals(f)){
count++;
}
if (currToken.equals(s)){
count++;
}
if (currToken.equals(i)){
count++;
}
line = reader.readLine();
}}
screen.println("The Cyclomatic Complexity of the file is: " + count);
}}
this works but reads straight into the comments made in the code so i am wondering if theres a line i can add that makes the program ignore the '//' which starts off each comment.Any ideas would be extremely greatfull.
Thanks

Hmmmmm - after:
while(line!=null)
something like:
if (line.startsWith("//") {
line = reader.readLine();
continue;
}
int spos = line.indexOf("//");
if (spos != -1) {
line = line.substring(0,spos);
}
Guy

![]() |
Protected Flash
|
File Array & Characte...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |