/* * utiity function(s) for Java programs */ package csci3366.sample.utility; public class Utility { /* * get integer from command-line argument. * prints error message(s) and exits program on error. */ public static int getIntegerArg(String[] args, int argIndex, int minVal, String description, String usageMsg) { if (args.length <= argIndex) { System.err.println(usageMsg); System.exit(1); } int rval = 0; try { rval = Integer.parseInt(args[argIndex]); } catch (NumberFormatException e) { System.err.println("non-numeric value for " + description); System.exit(1); } if (rval < minVal) { System.err.println(description + " must be at least " + minVal); System.exit(1); } return rval; } /* * get long integer from command-line argument. * prints error message(s) and exits program on error. */ public static long getLongArg(String[] args, int argIndex, long minVal, String description, String usageMsg) { if (args.length <= argIndex) { System.err.println(usageMsg); System.exit(1); } long rval = 0; try { rval = Long.parseLong(args[argIndex]); } catch (NumberFormatException e) { System.err.println("non-numeric value for " + description); System.exit(1); } if (rval < minVal) { System.err.println(description + " must be at least " + minVal); System.exit(1); } return rval; } }