Question: Goal: Develop a program that accurately counts the number of whitespace - separated words in a given file. The program should have the capability to
Goal:
Develop a program that accurately counts the number of whitespaceseparated words in a given file. The program should have the capability to obtain the input file's name either through command line arguments or user input prompt. Ensure comprehensive documentation of the chosen method. Validate the program's functionality by testing it with various text files, including the provided sample text file excerpttxt
Understanding:
We need to make a computer program that counts words in a file. The program should be able to get the file's name either from what the user types or from what's typed when starting the program. You have to explain clearly how you're getting the file's name. To check if the program works right, you have to try it with different files, including the one they gave you as an example excerpttxt
Code Changes Draft:
First we import necessary classes for file handling and user input.
The file name is either obtained from the command line or via user imput prompt. Leveraging the try and catch block the program reads the specified file and counts the spaceseparated words, provides a comprehensive error handling for FileNotFoundException and outputs the total word count on the console.
Changes attempts:
Initially I used a if block to prompt the user for the filename during program execution. If the command line arguments were provided then the first argument will be used as the filename, else user is prompted for the input.
Testing:
Tested the for loop to iterate through each word before importing helper class and then got stuck while incrementing each item and moved on to considering the while loop.
Added few print statements to print the output of each block of code to see the output and optimize the code accordingly.
Tested the final output by passing multiple text files to give the expected output, including excerpt.txt file ans specialchars.txt
Conclusion:
Throghout the code development I had a couple of challenges,
Importing helper classes and understanding how they work.
Troubleshooting the error while implementing the helper classes.
Overall, the changes were successful in achieving the desired functionality of accepting the filename either as a command line argument or through user input.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
