CMPS 134 Fall 2024
Prog. Assg. #4: CSV Query Application
Due: Tuesday, Oct. 22

Background

Wikipedia describes CSV files as follows:

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A CSV file typically stores tabular data (numbers and text) in plain text, in which case each line will have the same number of fields.

Although this does not seem to be standard terminology, we will use CSV-record to refer to a string that could play the role of a data record within a CSV file. That is, a CSV-record is a string that is intended to be interpreted as a sequence of fields, where each pair of consecutive fields is separated by a comma character (',').

The "class-list" files maintained by the University's Registrar are examples of CSV files. Each such file identifies the students who are enrolled in a particular section of a course and is accessible by the instructor of that course section. Each line/record in a class-list file is a CSV-record that describes a single student. Such a file looks something like this:

Example Class-List File
R01234567,Brown,Charlie,charlie.brown@scranton.edu,Freshman,Computer Science
R45678912,Dinkley,Velma,velma@scoobydoo.com,Senior,Paranormal Crime 
R55555555,Neuman, Alfred E.,alfred.neuman@madmagazine.com,,
R19283745,Van Pelt,Linus,linus.vanpelt@scranton.edu,Sophomore,Theology
R98765432,Van Pelt,Lucy,lucy.vanpelt@scranton.edu,Junior,Chemistry

As you can infer, the fields in each line/record in a class-list file provide, respectively, a student's Royal Number, last name, first name and (possibly) middle initial, e-mail address, class, and major. The record describing Alfred E. Neuman illustrates that a field's value can be the empty string. (The last two fields in his record have that value.)

The use of the comma as the separator character is just a convention, as other characters (e.g., ;, $, #) could play the role just as well, depending upon what kind of entities are being described in the CSV file. In the ideal, one would choose as a separator a character that could not possibly occur within any field. If no such choice is possible, we can allow for the separator to occur within a field by using an escape sequence, similar to how Java uses \" to describe an occurrence of the double quote character within a literal string.

For the purposes of this assignment, we will follow convention by choosing the comma character (i.e., ',') as our separator and we will assume that it cannot occur within any field. We will break from convention by assuming that a comma appears not only between consecutive fields but also after the last field. This will make your life a little easier!

Specification

Your primary task is to complete the stubbed method (namely, fieldOf()) within the Java application CSVQuery. (Less important tasks are described later.) As its name suggests, this program allows queries —albeit very simple ones— to be applied to CSV-records. For our purposes, a query is nothing more than a request to see the value of a specified field within a CSV-record. The field of interest is identified by its position within the CSV-record, where positions are assumed to be numbered beginning at zero. The method in question receives (via parameters) a CSV-record and a position number; its responsibility is to return the field occupying the specified position within the specified CSV-record.

The CSV-records that can be queried by the program are those in a CSV-file whose name is provided to it via a command line argument (or what in jGrasp terminology is called a "run argument"). Initially, the CSV-record subject to being queried is the first record in the file. The user can advance from one record to the next by making an appropriate response at the prompt. Similarly, the user can quit at any time.

Sample User/Program Interaction

To illustrate, suppose that the command-line argument given to the program was the name of the CSV-file whose contents are shown below, person_data.csv.

Example CSV-File
Smith,Karen,12 Main St.,Scranton,PA,18510,5'8",118,Blue,
Jones,Brian,46 Elm St.,Springfield,IL,34567,6'1,202,Brown,
Swift,Taylor,5 Brentwood,Hollywood,CA,98765,5'7,120,Blue,

Then here is one possible interaction with the program, with some explanatory notes to the right.

         Interaction                        Explanation
         -----------                        -----------
$ java CSVQuery person_data.csv         Command-line invocation of program

Enter query:>4                          Asking for value of 4th field
PA

Enter query:>2                          Asking for value of 2nd field
12 Main St.

Enter query:>1
Karen

Enter query:>N                          Asking to advance to next record

Enter query:>7
202

Enter query:>0
Jones

Enter query:>12
?                                       There is no 12th field

Enter query:>N

Enter query:>8
Blue

Enter query:>6
5'7

Enter query:>Q                          Asking to quit

Done!

Here are links to three other CSV-files that could serve you in testing your work: cartoon_chars.txt, Royals2024.txt, LadyRoyals2023.txt.


Secondary Tasks

In addition to completing the fieldOf() method, which is your primary task, you are also charged with making a few enhancements to the program, described now.

By examining the sample user/program interaction above, you will notice that the program accepts both 'N' and 'Q' as commands from the user. The former asks that the program advance to the next record in the input file. The latter asks that the program terminates. Modify the program so that

  1. it accepts 'R' as a command asking for the current record to be displayed, in full, and
  2. it accepts 'n', 'q', and 'r' as alternatives to their upper case counterparts.
    Hint: The String class has a toUpperCase() method. So does the Character class. The difference is that the former applies to, and returns, a String, whereas the latter applies to, and returns, a value of type char. A call to the latter would look like this: Character.toUpperCase(ch), where ch is of type char. Use of either method would make sense here.

As an "extra-credit" enhancement, make the program produce a message reporting the range of valid field positions for the current record whenever the user enters an invalid field position. For example, where the user entered 12 in the interaction shown above, the program would produce, rather than just a question mark, the more full response

?
The valid field positions are 0 through 7!


Program Submission

As usual, submit the relevant file —CSVQuery.java— to the appropriate Brightspace dropbox. Do not submit the associated .class file.

You may submit more than one time, but each time the name of the submitted file should be the same. Unless you make a request, expect that only the last file submitted will be examined by your instructor.

The program that was provided to you includes comments

Consider this to be a template that you are expected to follow in all subsequent programming assignments.