Answers
The answer to the above problem is as follows-
STEP 1 - First we will ask the user to enter the name of file where input text is. Since, the question does not have USAconst.txt file as its part, I have made the code generic so that it will work for all files. And used a sample file inputFile.txt for demonstration.
STEP 2 - The declare an int array to store the count of occurences of letters. It will store the number of A's at index 0, number of B's at index 1,....and so on till number of Z's at index 25
STEP 3 - Now inside try catch read the input file. Since, characters are read as ASCII values, so we will check if each character is a capital or small letter by comparing ASCII values.
And deduct 65 for capital and 97 for small to find its index in the count array and incrment that by 1. Keep doing it until entire file is read.
STEP 4 - Now in another try catch block write the output as given in question to letterCount.txt file and also write the same to the console.
JAVA CODE-
//import all necessary packages
import java.util.*;
import java.io.*;
//define the class
public class Main
{
//define the main method
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
//ask the user to enter the file name
System.out.print("Enter the file name : ");
String fileName = scn.nextLine(); //store the fileName
//create an object of File with for input file - fileName
File inputFile = new File(fileName);
//int array to store the count of occurences of letters
//it will store the number of A's at index 0, number of B's at index 1,....
//and so on till number of Z's at index 25
int[] count = new int[26];
//read the file inside try catch
try (FileReader fr = new FileReader(inputFile)) {
//to store the each letter of file. Character read from file are in ASCII form
int content;
//check if we have reached end of file
while ((content = fr.read()) != -1) {
//since content has ASCII value stored of corresponding Character letter
//check if content is between 65 and 90. For capital 'A' to 'Z'
if(content >=65 && content <=90){
count[content-65]++; //increment count at that index
}
//check if content is between 65 and 90. For small 'a' to 'z'
else if(content >=97 && content <=122){
count[content-97]++;
}
}
}catch (IOException e){ //catch block
e.printStackTrace();
}
//try catch block to write output file
try{
//object of FileWriter to write the output in letterCount.txt file
FileWriter fw = new FileWriter("letterCount.txt");
//iterate over the elements of count
for(int i=0;i<26;i++){
//since,we know that count stores the number of A's at index 0, number of B's at index 1,....
//and so on till number of Z's at index 25
//so convert index to Character letter by adding 65(ASCII for 'A') to i(index)
char ch = (char)(65+i);
//print the output on console
System.out.println("The occurence of " +ch+"'s is "+count[i]);
//write output to file
fw.write("The occurence of " +ch+"'s is "+count[i]+"\n");
}
fw.close();
}catch (IOException e){ //catch block
e.printStackTrace();
}
}
}
IMAGE OF CODE-
OUTPUT-
Since, the USAconst.txt file mentioned in question, is not given as part of question.
I've create a sample input text file named inputFile.txt for demonstration purposes.
Note - the code is generic and will work with any file name given as input.
Console-
letterCount.txt
If this answer helps, please give an up vote and feel free to comment for any query.
//and so on till number of z's at index 25 int[] count = new int[26]; //read the file inside try catch try (FileReader fr = new FileReader(inputFile)) { //to store the each letter of file. Character read from file are in ASCII form int content; // check if we have reached end of file while ((content = fr.read()) != -1){ //since content has ASCII value stored of corresponding Character letter // check if content is between 65 and 90. For capital 'A' to 'Z' if(content >=65 && content <=90){ count[content-65]++; //increment count at that index ad()) != -1e stored of For capital // check if content is between 65 and 90. For small 'a' to 'z' else if(content >=97 && content <=122){ count[content-97]++; } catch (IOException e) { //catch block e.printStackTrace();
Program finished with exit code 0 Press ENTER to exit console.