Last Updated: 2017-12-03 Sun 22:52

CSCI 1103 Lab13: Command Line Args / Scanning Files

CODE DISTRIBUTION: lab13-code.zip

  • Download the code distribution every lab
  • See further setup instructions below

CHANGELOG:

1 Rationale

Command line arguments provide a means by which the outside world can communication information to the a Java program at startup. One of the most common pieces of information to communication is the name of a file or files on which to operate. This lab makes use of this capability.

Scanners are often used in Java to read files and parse individual lines. This lab also covers how to go about this.

2 Download Lab Code and Setup

As in Lab01, download the code pack linked at the top of the page. Unzip this which will create a folder and create your files in that folder.

File State Notes
PARTNERS.txt Edit Fill in your name and the name of your partner in this text file
KTests.java Testing Utility routines for all tests
junit-1103.jar Testing For testing, don't try to edit this one
WCTests.java Testing Tests for problem 1
wordsNumbers1.txt Data Input data for tests
wordsNumbers2.txt Data Input data for tests
names1.txt Data Input data for tests
names2.txt Data Input data for tests
gettysburg.txt Data Lincoln's famous speech, input data for tests
WC.java Create Main file to create/code

3 Counting Lines, Words, Charactes

The classic wc utility in Unix counts the number of lines, words, and characters in files named on the command line. If you are on a Unix machine such as Mac OS X or Linux, you can open a terminal and determine the size of files according to these metrics using wc filename filename

> wc names1.txt 
 3  3 19 names1.txt                 // 3 lines, 3 words, 19 characters
> wc names2.txt 
  8  18 113 names2.txt              // 8 lines, 18 words, 113 characters
> wc gettysburg.txt 
  25  283 1511 gettysburg.txt       // 25 lines, 283 words, 1511 characters

The wc utility will also process all files listed on the command line which allows the above three invocations to be combined into the single run:

> wc names1.txt names2.txt gettysburg.txt 
   3    3   19 names1.txt
   8   18  113 names2.txt
  25  283 1511 gettysburg.txt
  36  304 1643 total

The goal of this lab is to reproduce parts of this utility's functionality in Java.

4 Class setup: WC

Create a class called WC in a file called WC.java. This class will contain only a main() method which will take a number of command line arguments. Each argument should be a filename to open and scan through to count lines, words, and characters.

Here are some example runs to illustrate the intent.

> java WC names1.txt                                // run on names1.txt
names1.txt : 3 lines, 3 words, 19 chars

> java WC names2.txt                                // run on names2.txt
names2.txt : 8 lines, 18 words, 113 chars

> java WC gettysburg.txt                            // run on gettysburg.txt
gettysburg.txt : 25 lines, 283 words, 1511 chars

> java WC names1.txt names2.txt gettysburg.txt      // run on all three of the above files
names1.txt : 3 lines, 3 words, 19 chars
names2.txt : 8 lines, 18 words, 113 chars
gettysburg.txt : 25 lines, 283 words, 1511 chars

Notice that when one file is named, it is processed but several files can be named such as the 3 that appear in the last example.

5 General Approach

Remember that in the main() method prototype

public static void main(String args[]) throws Exception

the variable args is an array of the other things that appear on the command line after class being run. In the above examples it would take on the following values:

> java WC names1.txt                                // run on names1.txt
// args[] = {"names1.txt"}

> java WC names2.txt                                // run on names2.txt
// args[] = {"names2.txt"}

> java WC names1.txt names2.txt gettysburg.txt      // run on all three of the above files
// args[] = {"names1.txt", "names2.txt", "gettysburg.txt"}

This means that main() should use a loop over the length of args[] opening each named file.

No exception handling is required: declare main() with throws Exception.

To count stats on each file remember the following:

  • Keep counts of lines, words, characters
  • Reset these to 0 each time you open a new file
  • Open a Scanner for each file and scan while there are more lines to read
  • Read each line using nextLine() and store it in a String variable
  • Open a Scanner on the line and use next() / hasNext() on that line scanner to count words in it.
  • Note that the length() of each line is how many characters it has EXCEPT for the \n newline character which is eliminated on calls to nextLine() so add on 1 when counting characters.
  • This will require nesting several loops
    • Outer over file names / command line args
    • Inner over lines in a single file
    • Innermost over words in a single line of a file
  • At the end of each file print its stats as indicated:
    gettysburg.txt : 25 lines, 283 words, 1511 chars
    

6 Getting Credit for this Lab

6.1 Demonstrate your code to Lab Staff (40%)

You should feel free at any time to get help from lab staff as you get stuck on the exercises.

By the end of the lab, make sure to demonstrate your code to a staff member. This will ensure that you receive full credit on the lab. Staff might ask you to

  • Change what is printed
  • Compile and run on the command line
  • Run tests in DrJava or on the command line
  • Show how to zip the folder with your programs in it

Be ready to do any or all of these.

6.2 Zip and Submit (60%)

Ensure that file PARTNERS.txt has your name and the name of your partner in it. This fill is located with the other Java files for the lab and can be edited with DrJava.

Once your are confident your code is working, you are ready to submit. Ensure your folder has all of the required files. Create a zip archive of your lab folder and submit it to blackboard.

On Canvas:

  • Click on the Assignments section
  • Click on the appropriate link for this lab
  • Scroll down to "Attach a File"
  • Click "Browse My Computer"
  • Select you Zip file and press OK

Author: Chris Kauffman (kauffman@cs.gmu.edu)
Date: 2017-12-03 Sun 22:52