HowMany.java


Below is the syntax highlighted version of HowMany.java from §1.4 Arrays.


/*************************************************************************
 *  Compilation:  javac HowMany.java
 *  Execution:    java HowMany str1 str2 ... strN
 *
 * HowMany takes a variable number of command-line arguments
 * and prints a message reporting how many there are.
 *
 *   % java HowMany
 *   You entered 0 command-line arguments.
 *
 *   % java HowMany Alice Bob Carol
 *   You entered 3 command-line arguments.
 *
 *   % java HowMany Alice
 *   You entered 1 command-line argument.
 *
 *************************************************************************/

public class HowMany {

    public static void main(String[] args) {

        // number of command-line arguments
        int N = args.length;

        // output message
        System.out.print("You entered " + N + " command-line argument");
        if ( N == 1 ) System.out.println(".");
        else          System.out.println("s.");
   }
}


Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:00:59 EST 2011.