Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,521 people online right now. Registration is fast and FREE... Join Now!




Character Count Method?

 
Reply to this topicStart new topic

Character Count Method?

mrdomino45
24 Apr, 2007 - 03:11 PM
Post #1

New D.I.C Head
*

Joined: 24 Apr, 2007
Posts: 1


My Contributions
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
User is offlineProfile CardPM
+Quote Post

vasdueva
RE: Character Count Method?
24 Apr, 2007 - 05:54 PM
Post #2

D.I.C Head
**

Joined: 3 Apr, 2007
Posts: 69


My Contributions
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
CODE

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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month