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

counting operaters from a text file

sukh singh
Greenhorn

Joined: Nov 30, 2004
Posts: 13
i was given help to create a code to count operaters such as:-
'+','-', '*', '/', etc.....
this works fine to count number of operaters which are a single character, but i a m having trouble counting operaters which are words such:-
"if", "else", "while", ect.....
i tried to modify the code counting single character operaters but i keep gettin a error.

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());
}
}
Oscar Reitsma
Greenhorn

Joined: Dec 03, 2004
Posts: 7
Method java.lang.String.toStringArray() does not exist.
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hello Sukh,
I'm not sure this is what you're looking for. But heres a simple program that counts the occurance of a given word (if) in a sentence. In your case, sReadLine will contain the first line as a String, right?. So, you can read the line and then pass that string into stringtokenizer instead of s, which i used.


Note, im a beginner in java but to the best of my knowledge, it should work ( i tried it and worked like a charm). i don't know how efficient is it though. But give it a go. Good Luck.

Mala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: counting operaters from a text file