Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

counting operaters from a text file

 
sukh singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method java.lang.String.toStringArray() does not exist.
 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic