Eighth lab: Arrays and counting key words

11 Mar 2022 - 11:10 | Version 1 |

Learning Objective(s)

  1. Learn to use one dimensional arrays
  2. Learn to use command line arguments
  3. Continue to improve skills working with files

Overview of the program

This lab should count the number of occurrences of different key words in a file.

Details

  • Input:
    • The first command line argument is a text file with all the key words in it. The words are separated by white space. The file can be multiple lines
    • The second command line argument is a text file with all sorts of words in it (multiple lines with multiple words per line). This is the file that is to be "counted".
    • You may assume there are no punctuation marks or numbers in either file. You are to ignore the differences between upper and lower case.
  • Restrictions
    • You may only read each file once.
  • Output
    • For each key word specified in the first file, there should be a line of output specifying the number of times (0 to n) it occurred in the second file.
    • The output should be sent to a file specified by the third command line argument
  • The program should be called WordCounter
  • You need to have at least one method besides the main method.
  • You must use at least one constant
  • You must use arrays as your primary storage
  • There will not be more than 1000 key words in the first file
  • All normal exceptions should be handled (not thrown)
  • Hints are at the end. I suggest you consider trying to solve without the hints. But they are there if you need/want them.

Sample "dialogue"

First file:

public class
int double

Second file: (note: no punctuation)

public class Demo 
    public static void main String args
        int a public

        return

Output file

3 public
1 class
1 int
0 double

Submission

  1. Create an empty zip folder.
  2. Place your program (.java file) in it
  3. Place your software development report in it.
  4. Submit the zip folder on canvas

Optional improvements

none this time

Hints/suggestions

Below are the steps I would recommend to take in solving the problem. Get each step to work correctly before moving on
  1. Have one key word (which can be inputted from the keyboard or a literal) and search a file (specified by a literal) for the number of times that keyword exists
  2. Add code before that to read in words from a file into an array (file specified by a literal). Assume there is one word per line. Then count the number of occurrences of the first word in the array in the second file
  3. Change it so that there can be multiple words in a line
  4. Add exception handling code whenever you feel comfortable. Also deal with the upper/lower case issue when you feel comfortable
  5. Change the "counting" routine to look up each word from the second file in the array, printing out the location.
  6. Add additional data structures to store counters and make the code do everything (except getting the filenames from the command line arguments)
  7. Add in command line arguments
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
This website is using cookies. More info. That's Fine