Write a program that counts and displays the number of times

Write a program that counts and displays the number of times each alphabetic character ‘a’ through ‘z’ appears in a text file. Do not distinguish between uppercase and lowercase characters, and ignore all other characters.

Solution

Please follow the code and comments for description :

CODE :

import java.io.File;
import java.util.Map; // required imports
import java.util.Scanner;
import java.util.TreeMap;

public class CountOccurrences { // class to run the code

public static void main(String[] args) throws Exception { // driver method that throws exception clause
  
TreeMap<Character, Integer> charMap = new TreeMap<>(); // map to save the data
File file = new File(\"input.txt\"); // read the file
Scanner sc = new Scanner(file); // get the sc object
System.out.println(\"The Occurrences of each Character is : \"); // print the data
while (sc.hasNext()) { // iterate over the data in the file
char[] chars = sc.nextLine().toLowerCase().toCharArray(); // make the letters to a single case
for (Character c : chars) { // for rach loop for the array of characters
if (!Character.isLetter(c)) { // check if the character is a letter or not
continue;
} else if (charMap.containsKey(c)) { // check if the map already has the character
charMap.put(c, charMap.get(c) + 1); // if so increment the value
} else {
charMap.put(c, 1); // else put the value
}
}
}
for (Map.Entry<Character, Integer> entry : charMap.entrySet()) { // looping to print the result to console
System.out.println(\"Character \" + entry.getKey() + \" - \" + entry.getValue() + \" times.\");
}
}
}


input.txt :

Java history is interesting to know. The history of java starts from Green Team. Java team members (also known as Green Team), initiated a revolutionary task to develop a language for digital devices such as set-top boxes, televisions etc.

For the green team members, it was an advance concept at that time. But, it was suited for internet programming. Later, Java technology as incorporated by Netscape.

Currently, Java is used in internet programming, mobile devices, games, e-business solutions etc. There are given the major points that describes the history of java


OUTPUT :

The Occurrences of each Character is :
Character a - 45 times.
Character b - 8 times.
Character c - 13 times.
Character d - 10 times.
Character e - 60 times.
Character f - 6 times.
Character g - 14 times.
Character h - 12 times.
Character i - 33 times.
Character j - 7 times.
Character k - 3 times.
Character l - 11 times.
Character m - 17 times.
Character n - 30 times.
Character o - 32 times.
Character p - 8 times.
Character r - 31 times.
Character s - 36 times.
Character t - 50 times.
Character u - 9 times.
Character v - 13 times.
Character w - 4 times.
Character x - 1 times.
Character y - 7 times.

Hope this is helpful.

Write a program that counts and displays the number of times each alphabetic character ‘a’ through ‘z’ appears in a text file. Do not distinguish between upperc
Write a program that counts and displays the number of times each alphabetic character ‘a’ through ‘z’ appears in a text file. Do not distinguish between upperc

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site