Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with a program that counts the number of occurrences of each letter in an array. I can't figure out how to display them

Need help with a program that counts the number of occurrences of each letter in an array. I can't figure out how to display them in order of occurrences and get the percentage. Thanks for your help still VERY new to this.

It should look like this.=

image text in transcribed

FULL INSTRUCTIONS

Create a program that prompts the user for a filename and verifies that the file exists and if not, inform the user and quit. If the file exits, it should then read it and count the number of occurrences of each letter from A through Z (regardless of case). Later, it should calculate and display the number of occurrences for each letter as well as the percentage of the total times that the letter appears in a table form sorted by highest to lowest occurrence. Finally, it should display the total number of letters found.

import java.io.*; import java.util.Scanner;

public class CountLettersBHarris {

public static void main(String[] args) { Scanner myInput = new Scanner(System.in); File myInputFile; String fileName; int charCount = 0; char temp; int[] freqs = new int[26]; char[] letter = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

System.out.print("Enter filename: "); fileName = myInput.nextLine();

myInputFile = new File(fileName);

if(!myInputFile.exists()) { System.out.println("File does not exist!"); System.exit(1); }

try( BufferedReader in = new BufferedReader(new FileReader(fileName))){ Scanner input = new Scanner(myInputFile); System.out.println("Letters' Frequencies"); System.out.println("-----------------------"); System.out.printf("%5s %5s %5s ", "Letter", "Freq","Percentage");

while((fileName = in.readLine()) != null){ fileName = fileName.toUpperCase(); for(char ch:fileName.toCharArray()){ if(Character.isLetter(ch)){ freqs[ch - 'A']++; } } for( int i = 0; i { temp = fileName.charAt( i );

if( temp != ' ' ) charCount++; } System.out.printf("%5s %5s %5s ", letter, freqs, percentage); } System.out.printf("Total letters:%d",charCount); input.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } }

Here's the sample txt file for testing.

"Hang the boy, can't I never learn anything? Ain't he played me tricks enough like that for me to be looking out for him by this time? But old fools is the biggest fools there is. Can't learn an old dog new tricks, as the saying is. But my goodness, he never plays them alike, two days, and how is a body to know what's coming? He 'pears to know just how long he can torment me before I get my dander up, and he knows if he can make out to put me off for a minute or make me laugh, it's all down again and I can't hit him a lick. I ain't doing my duty by that boy, and that's the Lord's truth, goodness knows. Spare the rod and spile the child, as the Good Book says. I'm a laying up sin and suffering for us both, I know. He's full of the Old Scratch, but lawsame! he's my own dead sister's boy, poor thing, and I ain't got the heart to lash him, somehow. Every time I let him off, my conscience does hurt me so, and every time I hit him my old heart most breaks. Wellawell, man that is born of woman is of few days and full of trouble, as the Scripture says, and I reckon it's so. He'll play hookey this evening, * and [* Southwestern for "afternoon"] I'll just be obleeged to make him work, tomorrow, to punish him. It's mighty hard to make him work Saturdays, when all the boys is having holiday, but he hates work more than he hates anything else, and I've GOT to do some of my duty by him, or I'll be the ruination of the child." Tom did play hookey, and he had a very good time. He got back home barely in season to help Jim, the small colored boy, saw nextday's wood and split the kindlings before supperat least he was there in time to tell his adventures to Jim while Jim did threefourths of the work. Tom's younger brother (or rather halfbrother) Sid was already through with his part of the work (picking up chips), for he was a quiet boy, and had no adventurous, troublesome ways. While Tom was eating his supper, and stealing sugar as opportunity offered, Aunt Polly asked him questions that were full of guile, and very deepfor she wanted to trap him into damaging revealments. Like many other simplehearted souls, it was her pet vanity to believe she was endowed with a talent for dark and mysterious diplomacy, and she loved to contemplate her most transparent devices as marvels of low cunning.

> run CountLetters Enter filename SampleFile.txt Letters' Frequencies Letter Freq Percentage E 187 O 158 T 155 A 139 S 118 I 116 H 113 N 108 92 80 76 65 51 48 43 40 38 10.54% 8.91% 8.74% 7.84 665% 6.54% 6.37% 6.09 5.19% 4.51% 4 . 28 3. 66% 2.87 2.718 2.42% 2.25% 2.14 1. 86% 1.80% 1.75% 1.47 0.96 0.28% 0.11% 0.06 32 31 26 17 2 Total letters: 1774

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions