Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is in java Write a program called CountTokens that: Prompts the user for th

ID: 3762477 • Letter: T

Question

this is in java

Write a program called CountTokens that: Prompts the user for the pathname of a file; Redirects Stdln to read from that file; Reads in the file's contents token by token (where tokens are separated by whitespace) and, for each token: creates an XMLToken object; adds the object to the end of an array list of XML tokens. Iterates throught the array list to count the number of words, opening tags, closing tags, and malformed tokens (that is, any token that is neither a word nor a tag). Prints the counts with explanatory text.

Explanation / Answer

In the java.io package:

     File f = new File("numbers.txt"); // numbers.txt is in the current directory

     File f = new File("cs305j/numbers.txt"); // file is in cs305j directory (relative path)

     File f = new File("C:/Settings and Documents/elvis/numbers.txt"); //absolute path

StreamTokenizer defines four int constants: TT_EOF, TT_EOL, TT_NUMBER, and TT_WORD. There are three instance variables. nval is a public double used to hold the values of numbers as they are recognized. sval is a public String used to hold the value of any words as they are recognized. ttype is a public int indicating the type of token that has just been read by the nextToken( ) method. If the token is a word, ttype equals TT_WORD. If the token is a number, ttype equals TT_NUMBER. If the token is a single character, ttype contains its value. If an end-of-line condition has been encountered, ttype equals TT_EOL. (This assumes that eolIsSignificant( ) was invoked with a true argument.) If the end of the stream has been encountered, ttype equals TT_EOF. The word count program revised to use a StreamTokenizer is shown here:

import java.io.*;
class WordCount {
public static int words=0;
public static int lines=0;
public static int chars=0;
public static void wc(Reader r) throws IOException {
StreamTokenizer tok = new StreamTokenizer(r);
tok.resetSyntax();
tok.wordChars(33, 255);
tok.whitespaceChars(0, ' ');
tok.eolIsSignificant(true);
while (tok.nextToken() != tok.TT_EOF) {
switch (tok.ttype) {
case tok.TT_EOL:
lines++;
chars++;
break;
case tok.TT_WORD:
words++;
default: // FALLSTHROUGH
chars += tok.sval.length();
break;
}
}
}
public static void main(String args[]) {
if (args.length == 0) { // We're working with stdin
try {
wc(new InputStreamReader(System.in));
System.out.println(lines + " " + words + " " + chars);
} catch (IOException e) {};
} else { // We're working with a list of files
int twords = 0, tchars = 0, tlines = 0;
for (int i=0; i<args.length; i++) {
try {
words = chars = lines = 0;
wc(new FileReader(args[i]));
twords += words;
tchars += chars;
tlines += lines;
System.out.println(args[i] + ": " +
lines + " " + words + " " + chars);
} catch (IOException e) {
System.out.println(args[i] + ": error.");
}
}
System.out.println("total: " +tlines + " " + twords + " " + tchars);
}
}
}