wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes count of operands Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

count of operands

Kishore
Greenhorn

Joined: Aug 26, 2002
Posts: 10
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Why not repost this as a new post in your original thread here? That way the discussion is all in one convenient place. I'll close this thread to prevent confusion.


"I'm not back." - Bill Harding, Twister
 
wood burning stoves
 
subject: count of operands
 
Similar Threads
count of operands
quick question: bufferedreader
help with counting operands and operaters
plz delete
Please help on Strings