/** * a little Java application class just for adding two numeric * command-line arguments (and demoing try-catch) * * * @author Sharon Tuttle * @version 2015-09-23 */ public class TryMe3 { /** * add its two command-lines and print result to screen * * @param args contains two "numeric" strings */ public static void main(String[] args) { // verify there are 2 arguments exactly if (args.length != 2) { System.out.println("MUST have 2 integer arguments!"); System.exit(1); } // ...otherwise, try to handle and add those // two arguments try { // note for future use: a local variable declared here // is LOCAL to the try block -- to be seen // in *all* the try and catch blocks, // declare such a variable BEFORE the try block! System.out.println(args[0] + " + "+ args[1] + " = " + (Integer.parseInt(args[0]) + Integer.parseInt(args[1]))); System.out.println("Whew!"); } catch (NumberFormatException exc) { System.out.println("Both arguments must be integers!"); } System.out.println("Good day and Goodbye!"); } }