//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** @author Casey Bowman * @version 1.1 * @date Mon Mar 02 13:45:27 EST 2015 * @see (License) MIT style license */ //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** Simple program for retrieving command line arguments and converting them. * The first argument must be an integer greater than zero and the second * argument must be a double greater than zero. * * e.g. > java ConvertingCommandLineArguments 13 25.2323 */ public class ConvertingCommandLineArguments { //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** Main method. String[] args is a String array containing the command * line arguments you want to use in your program. This program also uses * the switch statement as well as exception . * @see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html */ public static void main (String[] args) { int n = args.length; switch (n) { case 0: System.out.println ("You didn't enter any command line arguments!"); break; case 1: System.out.println ("You must enter an integer and a double!"); break; case 2: handleInput (args); break; default: System.out.println ("That's too many input arguments for me to handle!"); break; } // switch } // main //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** Method for handling the command line arguments. Uses exception handling * to assess if the input is of the correct type. * @param args String array of command line arguments. * @see http://docs.oracle.com/javase/tutorial/essential/exceptions/ */ public static void handleInput (String[] args) { int input1 = 0; double input2 = 0.0; try { input1 = Integer.parseInt (args[0]); } catch (Exception e) { System.out.println ("Your first input must be an integer!"); } try { input2 = Double.parseDouble (args[1]); } catch (Exception e) { System.out.println ("Your second input must be a double!"); } if (input1 > 0) System.out.println ("Your integer multiplied by 2 = " + (input1 * 2)); else System.out.println ("Your integer input didn't fit the requirements..."); if (input2 > 0.0) System.out.println ("Your double squared = " + (input2 * input2)); else System.out.println ("Your double input didn't fit the requirements..."); } // handleInput } // ConvertingCommandLineArguments