1 Replies - 14931 Views - Last Post: 24 April 2007 - 06:54 PM Rate Topic: -----

#1 mrdomino45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-April 07

Character Count Method?

Posted 24 April 2007 - 04:11 PM

I was wondering if there was a method in the java language that would allow me to read a file and count the number if characters like a's b's in a certin file? is there one ? Thanks in advance<3
Is This A Good Question/Topic? 0
  • +

Replies To: Character Count Method?

#2 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Character Count Method?

Posted 24 April 2007 - 06:54 PM

You can easily write one yourself. Process the file line by line. Use the indexOf() method to search out the character, once that character is found
you can us the indexOf(string,int) to search out if there is another occurence after the first character occured.
so
int charPos=0;
int charCount = 0;
char ch;
while(charPos != -1)
{
int charPos = (indexOf(ch, charPos) + 1);
if(charPos != -1)
{charCount++;)
}



Something like that should work, I added the +1 after the index of because if not you would enter an infinite loop, always returning the position of the first character in the string. Negative one is returned if no character is found.
Was This Post Helpful? 1

Page 1 of 1