Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

count of operands

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Get off me! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic