• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Counting one character at a time from a file

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to count and store the frequency each letter occurs form a .txt file . I am at the point where I can read back the file but I can not get any methods to work that would count the frequency of a letter in that file .


Simple ..no problems but here is some code that I think works in regard to counting the frequency but I am missing something to get it to function correctly ? Any ideas please let me know ! Thank you .



Again something where I think it would work but just do not see the error ?



So basically , I need to read a .txt file and print out that the character 'A' appeared x number of times .

Thank you again !
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G. Graz wrote:


That's a bad idea. The read method returns how many characters were actually read; it can be anywhere between 0 and letters.length. If it is less than letters.length, the remainder will contain the characters that were already in the array before the call to read (in this case '\0'). You'd better use the return of br.read(letters).

As for your problem, usually when counting occurrences a Map is used:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard idiom would be a loop like this:


Just to confuse you :-) , note that counting characters gets harder if the text can contain the full range of Unicode characters.
 
Your mother is a hamster and your father smells of tiny ads!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic