CPSC 124, Fall 2001
Lab 12: "String[] args" at Last

Since the beginning of the term, you have been seeing main() routines with the parameter list "(String[] args)". You now know that the String[] type represents an array of Strings. It's time to learn where the actual parameter for a main() routine comes from and how it can be used.

This week's lab is quite short, and you should be able to complete it during the lab period with some time left over to work on another project such as the third programming assignment, last week's lab, or your final project for the course.

This lab is due next Monday, along with last week's lab and the third programming assignment. For this lab, you should just make a printout of your program.

There are no starter files for this lab. You should create a program file from scratch.


Command-line Arguments

The main() routine of a stand-alone program is called by the system when a user tells the system to run the program. Generally, the user does this by typing in a command on the command line. Now, in fact, the user can include extra data on the command line. For example, instead of just typing "java prog" to run a program named prog, the user might type

         java prog Tom Dick Harry

The extra data items are referred to as command-line arguments for the program. The term "argument" here is synonymous with "parameter". The command-line arguments are parameters for the program as a whole. The system delivers these arguments to the program by putting them into an array of Strings and passing that array as the parameter to the main() routine. This array becomes the value of the formal parameter args in the main() routine:

         public static void main( String[] args ) { ...

So, in the main() routine, args.length is the number of command line arguments provided by the user, and the individual arguments are available as the array elements args[0], args[1], ..., args[args.length-1]. For the command line shown above, "java prog Tom Dick Harry", args.length would be 3, args[0] would be the string "Tom", args[1] would be "Dick", and args[2] would be "Harry".

The exercise for this lab is to write a short, complete Java program that uses command-line parameters. The name of the program should be "sum". The command-line arguments to the program will be integers. The program should add up the integers that are given on the command line and print the sum. For example, if the user runs the program by entering the command line

         java sum 17 42 112 3 7

then the program should output "The sum is: 181".

We did an example in class that adds up all the integers in an array of integers. If the name of the array is A, this can be done as follows:

         total = 0;
         for (int index = 0; index < A.length; index++) {
            total = total + A[index];
         }

For this program, however, you have an array of strings rather than an array of integers. In the previous lab, you saw how to convert a string to an integer. If str is a variable of type String, then this can be done as follows:

         int num;  // numerical value of string
         try {
            num = Integer.parseInt(str);
         }
         catch (NumberFormatException e) {
            // If we get here, the string does not contain a legal integer
         }

You can combine these techniques to add up the command line parameters of the program. The program should be able to deal in a reasonable way with errors in the input. If no command-line parameters are provided, the program should print an appropriate message and exit. If the program finds a command line parameter that is not an integer, it should print an appropriate message and exit.


Fun (?) with Command-line Arguments

This section is not a required part of the lab. Read it if you want to be more adept at using Linux.

In many cases, command-line arguments are file names. This is true for many built-in commands such as cp and rm. It's also true for the javac command that is used to compile Java source code files. These commands can work with multiple file names. For example, it is possible to compile two or more Java files at the same time with a command such as

         javac  Craps.java  Dice.java

and it is possible to copy multiple files, as long as the last command-line argument is the name of a directory. The files are copies into the directory. For example:

         cp  Craps.class  Dice.class  ~/www

This copies both Craps.class and Dice.class into the directory ~/www.

The bash shell (your user interface when you use the command line in Linux) has some built-in abbreviations that make it easy to work with multiple files. For example, the "wildcard" character * will match any sequence of characters in any file name. (Except for the "/" character -- this exception is made to avoid matching files in other directories unless you explicitely include a /.) If you use *.java, for example, it matches any file in the current directory that ends with ".java". If you use a * in a command-line argument, the bash shell automatically replaces that argument with every file name that matches. This means, for example, that saying

         javac  *.java

will compile every file in the current directory, and

         cp  Mosaic*  ~/www

will copy every file in the current directory that begins with "Mosaic" into the directory ~/www.

There are other characters that have special meaning in the bash shell. For example, a "?" is a wildcard that matches any single character (except /). And, of course, the space character is used to separate items on the command line. If you use one of these special characters in a file name, it looks like you are in trouble. Suppose you have a file named "File From Windows". If you say

         cp  File From Windows  trash

the shell will look for three files named "File", "From", and "Windows". You can have a file named "File From Windows", but to refer to it on the command line, you need to tell the bash shell not to give the space character its usual special meaning. There are several ways to do this. You could enclose the file name in single quotes:

         cp  'File From Windows'  trash

(Double quotes could also be used here, but there are a few characters that are treated as special even inside double quotes. Single quotes are safer.) Alternatively, but less readably, you could put a backslash (\) before the special characters:

         cp  File\ From\ Windows  trash

This is known as "escaping" the character.


David Eck, 15 November 2001