TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Count Letter Frequencies

Open a text file and count the frequencies of letters. Use a HashMap and BufferedReader.
Counting letter frequencies. Sometimes an analysis of a file's contents is helpful. A Java method can be used to read in a file, count letters, and display the results.
Example file. Let us first consider the example text file. It contains some patterns of letters like 2 x characters and one uppercase Z.
Example file: file.txt aaaa bbbbb aaaa bbbbb aaaa bbbbb CCcc xx y y y y y Z
This program uses BufferedReader and HashMap. With BufferedReader, it reads in a file line-by-line. We then loop over the line and call charAt to access characters.File

On each char: We test to see if we have a space—we avoid hashing spaces. We then put the char in the HashMap.

HashMap

GetOrDefault: We call this method to get an existing value from the HashMap or 0. Then we put one plus that value.

Finally: We use a for-loop on the result of keySet(). We display all characters found and their frequencies in the file.

For
Java program that uses HashMap import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; public class Program { public static void main(String[] args) throws IOException { HashMap<Integer, Integer> hash = new HashMap<>(); // Load this text file. BufferedReader reader = new BufferedReader(new FileReader( "C:\\programs\\file.txt")); while (true) { String line = reader.readLine(); if (line == null) { break; } for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (c != ' ') { // Increment existing value in HashMap. // ... Start with zero if no key exists. int value = hash.getOrDefault((int) c, 0); hash.put((int) c, value + 1); } } } // Close object. reader.close(); // Display values of all keys. for (int key : hash.keySet()) { System.out.println((char) key + ": " + hash.get(key)); } } } Output a: 12 b: 15 C: 2 c: 2 x: 2 y: 5 Z: 1
For performance, an array of all possible char values can be used. We can use a char as an index by casting it to an int. Then we increment each char index as it is encountered.Array
With some inspection, we can see that the results of the program are correct. This kind of program will need to be modified to suit your exact goals. But the general approach is durable.
Hashing, frequencies. This is an important tip—often in programs we can use a HashMap to count how often an element occurs. We can increment the value on each encountered key.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf