Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import java.io.

*;
import java.util.*;
public class TokenizingJavaSourceCode{
//public static final int TT_EOL;
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(Sys
tem.in));
System.out.print("Please enter a java file name: ");
String filename = in.readLine();
if(!filename.endsWith(".java")){
System.out.println("This is not a java file.");
System.exit(0);
}
File javaFile = new File(filename);
if(javaFile.exists()){
FileReader file = new FileReader(filename);
StreamTokenizer streamTokenizer = new StreamTokenizer(fi
le);
// It will go through the file and gives the number of t
okens in the file
int i=0;
int numberOfTokensGenerated = 0;
while(i != StreamTokenizer.TT_EOF){
i = streamTokenizer.nextToken();
numberOfTokensGenerated++;
}
System.out.println("Number of tokens = " + numberOfToken
sGenerated);
}
else{
System.out.println("File does not exist!");
System.exit(0);
}
}
}

You might also like