Here is the total code..i wrote till now..i was able to calculate the operator.. but i want a idea how to calculate the operands.
It didnt work using operators as delimiters.
Please help...
import java.io.* ;
import java.util.*;
public class Application1 extends Reader
{
Reader in = null;
int read = 0, charCount=0, wordCount = 0,
lineCount = 0;
int operators;
boolean whiteSpace = true;
char[] operat = {'+','-','/','*','%'};
public Application1 (Reader r)
{
in = r;
}
/**
* Implementation for parent's read method. Counts
* chars, words, and lines.
*/
public int read(char[] array, int off, int len)
throws IOException
{
if (array == null)
throw new IOException("Null array");
// Do actual read
read = in.read(array, off, len);
// Now count
charCount += read;
// Increment character count
char c;
for (int i=0; i < read; i++)
{
c = array[i];
if (c==operat[0]||c==operat[1]||c==operat[2]||c==operat[3]||c==operat[4])
operators++;
// Line count
if (c == '\n')
lineCount++;
//
Word count
if (Character.isWhitespace(c))
whiteSpace = true;
else
if (Character.isLetterOrDigit(c)
&& whiteSpace)
{
wordCount++;
whiteSpace = false;
}
}
return read;
}
public void close() throws IOException
{
in.close();
}
public int getCharCount() { return charCount; }
public int getWordCount() { return wordCount; }
public int getOperatorCount() { return operators; }
public int getLineCount() { return lineCount; }
/**
Test driver */
public static void main(
String args[]) throws Exception
{
Application1 cr = new Application1(new
FileReader("c:/test.txt"));
char c[] = new char[4096];
int read = 0;
while ((
read = cr.read(c, 0, c.length)) != -1)
System.out.print(new String(c, 0, read));
cr.close();
System.out.println(
"\n\nRead chars: " + cr.getCharCount() +
"\n words: " + cr.getWordCount() +
"\n lines: " + cr.getLineCount() +
"\n operators: " + cr.getOperatorCount());
}
}//end of class