Source of AddArgs.java


  2: /**
  3:  * This program adds up its command line arguments
  4:  *
  5:  * @author Mark Young (A00000000)
  6:  */
  7: public class AddArgs {

  9:     public static void main(String[] args) {
 10:         // introduce yourself
 11:         System.out.print("\n\n"
 12:                 + "Add Up My Command Line Arguments\n"
 13:                 + "--------------------------------\n\n"
 14:                 + "My " + args.length + " command line arguments were:\n");

 16:         // report your arguments
 17:         for (int i = 0; i < args.length; i++) {
 18:             System.out.printf("%4d: \"%s\"%n", i, args[i]);
 19:         }
 20:         System.out.println();

 22:         // add up your arguments
 23:         int sum = 0;
 24:         for (int i = 0; i < args.length; i++) {
 25:             // convert each argument to an integer and add it to the sum
 26:             sum += Integer.parseInt(args[i]);
 27:             // NOTE: might throw a NumberFormatException
 28:             // We'll just hope it doesn't!
 29:         }

 31:         // report the sum
 32:         System.out.println("The sum of those numbers is " + sum + ".\n");
 33:     }
 34: }