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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
PLEASE complete this in java. Write a program that prompts the user to enter a file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do i write a program to read muliple files? i can read one but i...

    How do i write a program to read muliple files? i can read one but i need to read 10: 0.txt - 9.txt Here is the code for reading one but i need to read 10 text files and print the frequencies of all the letters a - z, upper and lowercase, in one take. here is the output i should get #include <stdio.h> #include <stdlib.h> #include <string.h> #include <omp.h> int main() { double start_time = omp_get_wtime(); char str[1000); int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT