AddInts.java


Below is the syntax highlighted version of AddInts.java from §1.5 Input and Output.



/*************************************************************************
 *  Compilation:  javac AddInts.java
 *  Execution:    java AddInts
 *  Dependencies: StdIn.java
 *  
 *  This program takes a command line argument N, reads in N integers,
 *  and prints out their sum.
 *
 *  Note: you must hav the file StdIn.java in your working directory.
 *
 *  % java AddInts N
 *
 *************************************************************************/

public class AddInts
{ 
    public static void main(String[] args)
    { 
        int N = Integer.parseInt(args[0]);
        int sum = 0;
        for (int i = 0; i < N; i++)
            sum = sum + StdIn.readInt();
        System.out.println("Sum is " + sum);
   }
}


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Sep 29 16:17:41 EDT 2009.