1 answer

PLEASE complete this in java. Write a program that prompts the user to enter a file...

Question:

PLEASE complete this in java.

Write a program that prompts the user to enter a file name and displays the occurrences of each letter in the console window

Write a program that prompts the user to enter a file name and displays the occurrences of each letter in the console window and in a file. Letters are case insensitive. Use “USAconst.txt” for input and “letterCount.txt” for output. Use try- catch blocks to handle checked exceptions. Here is a sample file output: is ወ The ወ ው The Enter file name: USAconst.txt The occurrence of A's is 2675 The occurrence of B's is 612 The occurrenc The occurrence of D's is 1230 currenc 5107 The occurrence of F's is 1021 The occurre 444 The occurrence of H's is 2029 The occurrenc 2433 The occurrenc 96 occurrenc 53 The occurrence of L's is 1490 The occurrence of M's is 730 The occurrence of N's is 2630 The occurrenc 2729 The occurre 767 The occurrence of Q's is 46 The occurrenc 2206 The occurrence of S's is 2676 The occurrenc 3751 The occurre 848 The occurre 460 The occurrence of W's is 375 The occurrence of X's is 97 The occurrence of Y's is 504 The occurrence of Z's is 31 nnnnnnnnnnnnnnnnnnnnnnnn ወ ወ ወ ወ ወ

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-

1 //import all necessary packages import java.util.*; 3 import java.io.*; 4 //define the class 5 public class Main 8- //defin

ооо у про меоо бо уділ воё оо оо //try catch block to write output file try{ //object of FileWriter to write the output in le

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.

Main.java inputFile txt letterCount.txt 1 AB CD EFGH IJK LMNOPQRS TUV WXYZ 2 ab cd efgh ijk lmnopqrs tuvwxyz

Note - the code is generic and will work with any file name given as input.

Console-

Enter the file name : input File.txt The occurence of As is 2 The occurence of Bs is 2 The occurence of Cs is 2 The occure

letterCount.txt

va inputFile.txt letterCount.txt The occurence of As is The occurence of Bs is 2 The occurence of Cs is 2 The occurence of

If this answer helps, please give an up vote and feel free to comment for any query.

.

Similar Solved Questions

1 answer
Is this molecule acidic or basic?
A)Metronidazole B) Which group will more readily undergo hydrolysis-ester or carbonate if they are in the same molecule?...
2 answers
A palindrome is an integer that reads the same forward and backwards
A palindrome is an integer that reads the same forward and backwards. For example, 31213 is a 5 digit palindrome. How many 3 digit palindromes are even? (A) 30 (B) 36 (C) 40 (D) 45 (E) 50...
1 answer
Question 12 Assuming a discount rate of 7.25%, in how many years' time will R7 000...
Question 12 Assuming a discount rate of 7.25%, in how many years' time will R7 000 be worth R20 000? (1) 9 years (2) 10 years (3) 13 years (4) 15 years (5) 20 years...
1 answer
Profit maximizing firms will increase production of their product up to the point where AVC =...
Profit maximizing firms will increase production of their product up to the point where AVC = ATC. O P = ATC. OP=AVC. MR = MC....
1 answer
Question 2 2.5 pts When a capacitor C stored in it? 50μF is charged to 25...
Question 2 2.5 pts When a capacitor C stored in it? 50μF is charged to 25 volts, how much electric charge (in unit of micro columb) is...
1 answer
~ 〉' b, sin a. Find the Fourier coefficients for the function f(x)=| 7, 2 0 x〉 2 ~ 〉' b, sin a. Find the Fourier coefficients for the function f(x)=| 7, 2 0 x〉 2
~ 〉' b, sin a. Find the Fourier coefficients for the function f(x)=| 7, 2 0 x〉 2 ~ 〉' b, sin a. Find the Fourier coefficients for the function f(x)=| 7, 2 0 x〉 2...
1 answer
Please set eval = FALSE in the codechunk of your RMarkdown, as the output for this...
Please set eval = FALSE in the codechunk of your RMarkdown, as the output for this question will be too lengthy. In R, use set.seed (35135) and then the rnorm) command to generate 80,000 standard normally distributed observations. Put those values into a matrix with 400 rows and 200 columns. Now fit...
1 answer
Explain the stakeholders theory of a firm?
Explain the stakeholders theory of a firm?...
1 answer
Example of a map on how to develped a markitnplan for healthacarenfacility
example of a map on how to develped a markitnplan for healthacarenfacility...
1 answer
REVIEW SHEET FOR E2 IN CHEM 3153, OCTOBER 27, 2019 Draw the structure for the MAJOR...
REVIEW SHEET FOR E2 IN CHEM 3153, OCTOBER 27, 2019 Draw the structure for the MAJOR ORGANIC PRODUCT form each reaction. Assume the reactions have been terminated and neutralized. a. R-COR + DIBAL-H in hexane/-78 'C ------------ OR 1. CO2 b. Ph-Br + Mg/ether - -> 2. H,0* THE c. methyl butanoat...
1 answer
11. (5 pt) For a 1 mCi sample, what is the activity of 18F in Bq?...
11. (5 pt) For a 1 mCi sample, what is the activity of 18F in Bq? 18F decays by positron emission with a half-life of 109 minutes. a. 3.7 x 104 Bq b. 3.7 x 107 Bq c. 3.7 x 1010 Bq d. 2.7 x 10-3 Bq e. 2.7 x 10-11 Bq...
1 answer
You have a bottle of water. The water has a mass of 35 grams. You heat...
You have a bottle of water. The water has a mass of 35 grams. You heat the water with 125 calories of heat and the final temperature of the water is 42°C. What was the initial temperature (in Celsius) of the water before you added the heat? The specific heat of water is 1 calorie / (°Cg)....
1 answer
With reference to the circuit shown below, think superposition and find that part of VI() due...
With reference to the circuit shown below, think superposition and find that part of VI() due to the voltage source acting alone. Round your final answers to two decimal places. Leave any angle 0 as 0% 0 360°. 1.1 2 0.8 mF 1.1? 1 mH 30 cos 500 t 20 sin 500 t COS t +...
1 answer
Figure 3 shows a schematic arrangement of an in-line tension meter, commonly used to sense the te...
PART B pls ty Figure 3 shows a schematic arrangement of an in-line tension meter, commonly used to sense the tension T across a cable. The cable is routed through three pulleys (P1, P2 and Ps). As shown in Figure 3, only one of the pulleys (P2) is attached to the free end of a cantilever beam, senso...
1 answer
Core: 0 of 1 pt 5-60 (similar to) localed on the top-nght comer of the dala...
core: 0 of 1 pt 5-60 (similar to) localed on the top-nght comer of the dala table below in order to copy its contorls into a spreadsheet 12 of 13 (10 HW Score: of years to a given return In the of setun on he inta...
1 answer
What is the IRR of the following set of cash flows?    Year Cash Flow 0...
What is the IRR of the following set of cash flows?    Year Cash Flow 0 –$10,310             1 3,100             2 6,600        ...
1 answer
Part A What is the numerical value of Kap if the equilibrium mixture has [Cu2+] and...
Part A What is the numerical value of Kap if the equilibrium mixture has [Cu2+] and [CO, Express your answer using two significant figures equal to 12x10-5 M2 | ??? Submit Request Answer...
1 answer
XYZ Company began operations in May, 2022 by selling common stock to owners in exchange for...
XYZ Company began operations in May, 2022 by selling common stock to owners in exchange for $90,000 cash. During 2022, ABC Company entered into the following transactions: 1. On May 23, ABC Company purchased inventory for $50,000 cash. 2. On June 1, ABC Company purchased a three-year insurance polic...
1 answer
The spaceship Intergalactica lands on the surface of the uninhabited Pink Planet, which orbits a rather...
The spaceship Intergalactica lands on the surface of the uninhabited Pink Planet, which orbits a rather average star in the distant Garbanzo Galaxy. A scouting party sets out to explore. The party's leader–a physicist, naturally–immediately makes a determination of the acceleration d...