Its a textbook problem I need to figure out how to make a co
It\'s a textbook problem. I need to figure out how to make a code in Java.
Still, don\'t understand how to read in the text file into netbeans.
(Occurrences of each letter) Write a program that prompts the user to enter a file name and displays the occurrences of each letter in the file.
Letters are case-insensitive. Here is a sample run:
Enter a filename : Lincoln.txt
Number of A\'s : 56
Number of B\'s : 134
...
Number of Z\'s : 0
Solution
Please follow the code and comments for description :
CODE :
import java.util.*; // required imports for the code
import java.io.*;
public class CharCount { // class that calculates the count of the characters irrespective of the case
public static void main(String[] args) throws IOException { // main method that drives the program
Scanner sc = new Scanner(System.in); // scanner class that reads the data from the user
System.out.println(\"Please Enter the FileName to Calculate the Total Occurrences of Every Character : \"); // prompt for the user to enter the filename
String filename = sc.nextLine();
File file = new File(filename+\".txt\"); // opening the file based on the user choice
try {
BufferedReader reader = new BufferedReader(new FileReader(file)); // buffered reader class to open the file and read the data
TreeMap<Character, Integer> map = new TreeMap<>(); // map that stores the key value pairs
String line = null;
while ((line = reader.readLine()) != null) { // reading the file line by line till it reaches the end of the file
for (char ch : line.toCharArray()) { // iterating over the characters in the line
ch = Character.toUpperCase(ch); // making the characters to a single case
if (ch == \' \') { // ignoring the spaces in the file
continue;
}
if (Character.isDigit(ch)) { // ignoring the integer values
continue;
}
if (map.containsKey(ch)) { // checking for the characters
int val = map.get(ch);
map.put(ch, val + 1); // adding to the map by incrementing the value if already exists
} else {
map.put(ch, 1); // if not the new one is added to the map
}
}
}
for (char key : map.keySet()) { // iterating over the map
System.out.println(\"Number of \" + key + \"\'s are : \" + \" = \" + map.get(key)); // printing the data to console
}
} catch (FileNotFoundException e) { // catching the exceptions while dealing with the file
// File not found
System.out.println(e);
} catch (IOException e) { // checking for the input output operations and functions
// Couldn\'t read the file
System.out.println(e);
}
}
}
input.txt :
Program that prompts the user to enter a file name and displays the occurrences of each letter in the file
Letters are case insensitive
OUTPUT :
Please Enter the FileName to Calculate the Total Occurrences of Every Character :
input
Number of A\'s are : = 9
Number of C\'s are : = 5
Number of D\'s are : = 2
Number of E\'s are : = 20
Number of F\'s are : = 3
Number of G\'s are : = 1
Number of H\'s are : = 5
Number of I\'s are : = 7
Number of L\'s are : = 5
Number of M\'s are : = 3
Number of N\'s are : = 7
Number of O\'s are : = 5
Number of P\'s are : = 4
Number of R\'s are : = 10
Number of S\'s are : = 9
Number of T\'s are : = 13
Number of U\'s are : = 2
Number of V\'s are : = 1
Number of Y\'s are : = 1
Hope this is helpful.

