Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that analyzes the contents of a text file and produces the follo

ID: 3607237 • Letter: W

Question

Write a program that analyzes the contents of a text file and produces the following results: 1) the number of words in the text file; 2) the number of unique words in the text file: 3) the number of characters in the text file; 4) the number of occurrences of each lower case letter in the alphabet; 5) the number of occurrences of each upper case letter in the alphabet; and 6) the frequency of each letter, both upper case and lower case, in the alphabet. Make sure you organize and display your results in an appropriate format that is easy to read. The program should prompt the user for the name of a text file and then analyze the text to generate the results described above. If the file does not exit, the program should report the error and give the user another try to enter another file name. If the number of tries exceeds 5, the program should display an appropriate error message and exits. Use functions to make the development process manageable and structure your code into multiple files. Make sure you document the functions you introduce by writing concise, precise and clear comments before you introduce the prototype of each function. You work in pairs. Each student must demonstrate evidence of significant contributions to receive credits for the project.

Explanation / Answer

import java.io.*;
import java.util.*;
public class Main {

public static void main(String[] args) {
int charCount = 0;
int lowerCharCount = 0;
int upperCharCount = 0;
//Words dictionary
Map<String, Integer> words = new HashMap<String, Integer>();
//Character dictionary
Map<Character, Integer> chars = new HashMap<Character, Integer>();
int flagCount = 5;
//Checking user exceeded the file name entry chances or not
while(flagCount>0){
try {
  
System.out.print("Enter the file name : ");
//Taking filename from user to open
Scanner input = new Scanner(System.in);
//File open
File file = new File(input.nextLine());
//Opening file to Stream to read contents
input = new Scanner(file);
  
//checking file ended or not
while (input.hasNextLine()) {
String line = input.nextLine();
//Getting words in each Line
String wordList[] = line.split(" ");
for(int i=0;i<wordList.length;i++){
//Updating words in dictionary with their count
if(words.containsKey(wordList[i])){
words.put(wordList[i],words.get(wordList[i])+1);
}
else{
words.put(wordList[i],1);
}
//Getting characters in each word
char charsInWord[] = wordList[i].toCharArray();
for(int j=0;j<charsInWord.length;j++){
//Updating characters in dictionary with their count
if(chars.containsKey(charsInWord[j])){
chars.put(charsInWord[j],chars.get(charsInWord[j])+1);
}
else {
chars.put(charsInWord[j],1);
}
}
//Updating characters count in the line
charCount = charCount + wordList[i].length();
}
}
//Calculating total words count in file
float wordCount = 0.0f;
for (float f : words.values()) {
wordCount += f;
}
//Result for 1 sub-problem
System.out.println("The number of words in the text file : "+wordCount);
//Result for 2 sub-problem
System.out.println("The number of unique words in the text file : "+words.size());
//Result for 3 sub-problem
System.out.println("The number of characters in the text file : "+charCount);
//Result for 4 sub-problem
System.out.println("The number of occurrences of each lower case letter in the alphabet : ");
//Getting each char in characters dictionary
Set set = chars.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
Character ch = (Character)mentry.getKey();
//Checking whether Character is lower or not
if(Character.isLowerCase(ch)){
System.out.println(ch+"->"+mentry.getValue());
}
}
//Result for 5 sub-problem
//Getting each char in characters dictionary
System.out.println("The number of occurrences of each upper case letter in the alphabet : ");
iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
Character ch = (Character)mentry.getKey();
//Checking whether Character is upper or not
if(Character.isUpperCase(ch)){
System.out.println(ch+"->"+mentry.getValue());
}
}
//Result for 6 sub-problem
System.out.println("the frequency of each letter, both upper case and lower case, in the alphabet : ");
iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
Character ch = (Character)mentry.getKey();
if(Character.isUpperCase(ch)){
int count = chars.get(ch)+chars.get(Character.toLowerCase(ch));
System.out.println(ch+"->"+count);
}
}
  
input.close();
flagCount = 0;
System.exit(0);
//Handling FileNotFoundException
} catch (FileNotFoundException e) {
flagCount--;
System.out.println("File not available. Remaining chances : "+flagCount+" Please try again.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
System.out.println("You exceed the maximum chances to enter filename");
}
}
/* Sample input in contents.txt (which is passed as file in sample run)

An official launch event was to be held on 12 December 2015, coinciding with Rajinikanth's birthday. However, the team chose to avoid publicity as a result of the 2015 South Indian floods.[45] Instead, the team held a low key launch event at the AVM Studios on 7 December, with the director and the producers in attendance. Titled 2.0, the film then began its first scheduled shooting on 16 December 2015 at a set erected in the outskirts of Chennai at EVP World.[46][47][48] On the first day of the shoot, a scene featuring Rajinikanth and several dwarf actors was shot at the erected set, while the team's principal cast and crew also assembled for a photo shoot.[49]
The first schedule of the film, consisting of Rajinikanth and Amy Jackson, continued in Chennai until 30 December 2015.[50] The team then worked on a second schedule throughout the middle of January 2016 in Chennai and shot scenes featuring Rajinikanth at Mohan Studios and by Madras Boat Club.[51] Shankar continued filming portions not involving the lead actors throughout February 2016 in Chennai, with a car chase sequence shot in Royapettah.[52] Another schedule to shoot a song was initially set to be held at Salar de Uyuni in Bolivia but was canceled due to bad weather, and the team opted not to travel to the country.[53]

*/
/* Sample Output 1:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote