my dog learned polymorphism
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
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

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

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 561


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: 3152

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

Joined: Oct 14, 2008
Posts: 1337
This is a somewhat similar question:
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46425
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:
subject: Highest Frequency of Letter(s) in each Line of File.
It's not a secret anymore!