File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Highest Frequency of Letter(s) in each Line of File. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Highest Frequency of Letter(s) in each Line of File." Watch "Highest Frequency of Letter(s) in each Line of File." New topic
Author

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

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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.


When The Going Gets Tougher,The Tougher gets Going
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
This is a somewhat similar question: http://www.coderanch.com/t/517116/java/java/String-java
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Highest Frequency of Letter(s) in each Line of File.