Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Highest Frequency of Letter(s) in each Line of File.

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers,

Need Java Advice and Code which reads a file(File.txt) and prints to the console a list of all the letters that occurred with the highest frequency in each line of the file, followed by the frequency.
The list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters.



-------------------------------
Sample Input File(File.txt)
-------------------------------
When riding your bicycle backwards down a one-way street, if the
wheel falls of a canoe, how many ball bearings does it take to fill
up a water buffalo?
Hello Howard.

-----------------
Sample Output
-----------------
e 6
al 7
a 3
Hlo 2

Need help Ranchers.I'm struggling to get this right.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way would be:
You can have a list of HashMap where there's on HashMap for each line. If the letter is a new one- Create a new entry in the Map with value=1, if its an already existing letter- then increment the value. Then you can search to get the keys(letters) with the maximum value.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are NotACodeMill so you need to show some effort. I'll give you a great hint: a char can be used as an array index. Use it in combination with the increment operator.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a somewhat similar question: http://www.coderanch.com/t/517116/java/java/String-java
 
Campbell Ritchie
Sheriff
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't remember the link just at the moment, but there is something in the Maps part of the Java™ Tutorials Collections section about frequency of words in a sentence. That might be similar to your problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic