Computing.Net > Forums > Programming > JAVA String Tokenizer problem!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

JAVA String Tokenizer problem!

Reply to Message Icon

Name: Pearljam04
Date: November 19, 2002 at 15:35:15 Pacific
OS: win 2000
CPU/Ram: amd 1.2ghz/512mb
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: Guy
Date: November 20, 2002 at 06:33:11 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More


Protected Flash File Array & Characte...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: JAVA String Tokenizer problem!

Java String Problem www.computing.net/answers/programming/java-string-problem/9097.html

String Tokenizer www.computing.net/answers/programming/string-tokenizer/6918.html

String Tokenizer Problem www.computing.net/answers/programming/string-tokenizer-problem/10817.html