Count the Occurrence of Strings

The purpose of this project:

  • Get you back to java programming.
  • Set up the working environment for this semester.
  • Use of array, String, file I/O, and simple control structures.
  • Implement interface Comparator to sort the objects.
  • In order for you to show us that you can do simple java programming, you are only allowed to use the following built-in java classes:

  • String,
  • FileReader, BufferedReader,
  • Integer,
  • System,
  • Exception,
  • Arrays

  • If you need to use more than what listed above, please email: Dr. Maccabe: maccabe at cs.unm.edu or Wenbin Zhu: wenbin at cs.unm.edu. (please change the at to @ for the email addresses).

    Description of the project:

    In this warm-up project, you will write a java program that reads from a file. The first line of the file is an integer, it tells you how many types of strings in this file. Each of the other lines of the file is a string, which only contains lower case letters from a to z. Your program counts the strings and sorts the strings as required by the command line arguments. Following is a sample of how the file, sample.txt, looks like:

    3
    smile
    smile
    confused
    confused
    sad
    confused
    confused
    smile
    confused
    smile
    confused
    smile

    Your java program implements the class CountString. It accepts two arguments from the command line: file name and printing format. The printing format is an integer of 0, 1, or 2.

  • For type 0, your program should print the strings as well as their occurence count sorted by their appearance in the input file.
  • For type 1, your program should print the strings as well as their occurence count sorted by occurence count in decreasing order.
  • For type 2, your program should print the strings as well as their occurence count sorted in alphabetic order.

    How your program runs:

    The following shows how your program responds to the command line inputs:


     % java CountString sample.txt 0
     smile      5 
     confused   6
     sad        1 
    
    % java CountString sample.txt 1
     confused   6
     smile      5 
     sad        1 
    
    
    % java CountString sample.txt 2
     confused   6 
     sad        1 
     smile      5 

    What you need to turn in:

  • CountString.java
  • readme
  • Please read the file format for your program.


    Back to Prof. Maccabe's cs251 Home Page.