aspose file tools*
The moose likes Java in General and the fly likes help with counting operands and operaters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help with counting operands and operaters" Watch "help with counting operands and operaters" New topic
Author

help with counting operands and operaters

sukh singh
Greenhorn

Joined: Nov 30, 2004
Posts: 13
hi can any one help me with counting the operands and operators of a source code from a text file

[ November 30, 2004: Message edited by: sukh singh ]


the code below is what i have got working so far, but i dont know how to count slected operands and operators form a text file

[ November 30, 2004: Message edited by: sukh singh ]

[ Jess added UBB [code] tags to perserve whitespace ]
[ December 01, 2004: Message edited by: Jessica Sant ]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

sukh-

This sounds a lot like a homework assignment. We're not here to do your homework for you, we're here to help you understand Java better and help you to help yourself. So -- what have you tried already? What are your ideas about how to solve this? Tell us what you've attempted and then we'll try to help you find a better solution.

- Jess
Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
Hi Sukh Singh,
I would help you out, if you could be a bit precise at explaining your problem. And also much better, with an example so as to let me know what exactly you wanted to do.

- SAi.
sukh singh
Greenhorn

Joined: Nov 30, 2004
Posts: 13
i am having problems in counting the operaters in a source code which has been saved as a text file.
i have to set set certain operaters like "end", "if", "+", "*" etc... in arraylist and then i need to compare these operaters to the text file and see if they appear in the source code and then count how many times they appear in the source code.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
Taking an input stream apart into tokens is frequently handled by a java.io.StreamTokenizer
I think that is what Java compilers use since it is set up to recognize quoted strings and various comment styles.
Bill
sukh singh
Greenhorn

Joined: Nov 30, 2004
Posts: 13
i just dont know where to start it.
i know i have to create a array holding the operaters i want to count, buti dont know what to do next.
i've tried to use the code, which i have shown above as a guidline to count the operaters, but i keep messing up. i am able to read from the text file, but i am unable to compare the words in the text file to the words/operaters in the array list and count them.

for example if had the following operaters in my araylist
if, else, while, +, -, *, etc....
how can i count how many times each operater appears in a source code from a text file.
Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
Hi Sukh,
I just wrote some code for you.
Hope this fits your need.
Bye,
SAi
=================================================
=================================================
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/*
* Created on Dec 2, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author sainath veepuri
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/


public class WordCount
{

int iPlusCount = 0;
int iMinusCount = 0;
int iStarCount = 0;


// Sukh, you can change this array according to your need.
char cOperatorsArray[] = {'+','-','*'};


private int getPlusCount(){
return iPlusCount;
}

private int getMinusCount(){
return iMinusCount;
}

private int getStarCount(){
return iStarCount;
}

private void readMyFile(String sFile){
try {
try {
FileReader fReader = new FileReader(sFile);
BufferedReader bReader = new BufferedReader(fReader);
String sReadLine = null;
while((sReadLine = bReader.readLine()) != null){
countOperators(sReadLine, cOperatorsArray);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}

private void countOperators(String sReadLine, char[] cArrayOperators){
char[] cReadline = sReadLine.toCharArray();
int iReadLineLength = cReadline.length;
int iNoOfOperators = cArrayOperators.length;

for(int i=0; i<iReadLineLength; i++){
for(int j=0; j<iNoOfOperators; j++){
if(cReadline[i] == cArrayOperators[j]){

// Sukh, you can add the loops according to your need here.
if(cReadline[i] == '+'){
iPlusCount = iPlusCount + 1;
}
else if(cReadline[i] == '-'){
iMinusCount = iMinusCount + 1;
}
else if(cReadline[i] == '*'){
iStarCount = iStarCount + 1;
}

}
}
}
}



public static void main(String[] args)
{
WordCount wCount = new WordCount();
wCount.readMyFile("C:\\Documents and Settings\\sainathv\\Desktop\\sai.txt");
System.out.println("- count is --> "+wCount.getMinusCount());
System.out.println("+ count is --> "+wCount.getPlusCount());
System.out.println("* count is --> "+wCount.getStarCount());
}
}
=================================================
=================================================
sukh singh
Greenhorn

Joined: Nov 30, 2004
Posts: 13
i tried to modify the code you sent so i could count operaters like "end", "if", "while", etc.....
but i keep gettin errors, the program wont accept a string as a operater.

i get the following error:

H:\qp&e\1\StringCount.java:52: cannot resolve symbol
symbol : method toStringArray ()
location: class java.lang.String
String[] cReadline = sReadLine.toStringArray();
^
1 error
Finished


here is the code which i have tried to use:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

//*******************************************************************************************************************************

public class StringCount
{

int iADDHANDLERCount = 0;

//*******************************************************************************************************************************


// change this array according to your need.
String sOperatorsArray[] = {"ADDHANDLER"};


//*******************************************************************************************************************************


private int getADDHANDLERCount(){
return iADDHANDLERCount;
}


//*******************************************************************************************************************************


private void readMyFile(String sFile){
try {
try {
FileReader fReader = new FileReader(sFile);
BufferedReader bReader = new BufferedReader(fReader);
String sReadLine = null;
while((sReadLine = bReader.readLine()) != null){
countOperators(sReadLine, sOperatorsArray);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}


//*******************************************************************************************************************************

private void countOperators(String sReadLine, String[] sArrayOperators){
String[] cReadline = sReadLine.toStringArray();
int iReadLineLength = cReadline.length;
int iNoOfOperators = sArrayOperators.length;

for(int i=0; i<iReadLineLength; i++){
for(int j=0; j<iNoOfOperators; j++){
if(cReadline[i] == sArrayOperators[j]){

// add the loops according to your need here.
if(cReadline[i] == "ADDHANDLER"){
iADDHANDLERCount = iADDHANDLERCount + 1;
}

}
}
}
}


//*******************************************************************************************************************************



public static void main(String[] args)
{
StringCount sCount = new StringCount();
sCount.readMyFile("H:/qp&e/1/Shapes.txt");
System.out.println("ADDHANDLER count is --> "+sCount.getADDHANDLERCount());
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with counting operands and operaters