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

Write a program called CountLetters. java that prompts the user for the name of

ID: 3828745 • Letter: W

Question

Write a program called CountLetters. java that prompts the user for the name of a text file, reads in all of the text in that file, and then computes and prints a table of letter frequencies. For example, if the file is the text of the U.S. constitution, which you downloaded for the CountLetters program, the program will print: Enter the name of the file: data/constitution.txt a 2, 660 b 611 c 1, 190 d 1, 223 e 5, 108 f 1, 018 g 439 h 2, 019 i 2, 486 j 95 k 53 l 1, 478 m 725 n 2, 635 o 2, 739 p 764 q 46 r 2, 187 s 2, 693 t 3, 755 u 843 v 473 w 372 x 124 y 503 z 31

Explanation / Answer

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class CountLetters {

  

   public static void main(String[] args) throws IOException {

      

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter input file name: ");

       String fileName = sc.next();

      

       FileReader inputStream = new FileReader(new File(fileName));

      

       int[] count = new int[26];

      

       int c;

while ((c = inputStream.read()) != -1) {

if(Character.isLetter(c)){

   c = Character.toLowerCase(c);

   count[c - 97]++;// increasing count of current character

}

}

  

inputStream.close();

sc.close();

  

for(int i=0; i<26; i++){

   System.out.println(((char)(i+97)) + " "+count[i]);

}

   }

}

/*

Sample run:

Enter input file name: test.txt

a       20

b       3

c       9

d       3

e       23

f       4

g       5

h       14

i       14

j       3

k       3

l       8

m       3

n       21

o       11

p       5

q       0

r       15

s       17

t       23

u       11

v       1

w       5

x       1

y       4

z       0

*/

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