SHARE
TWEET

Untitled

a guest Oct 19th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Driver.java
  3.  *
  4.  * The driver for our command line argument example
  5.  *
  6.  * @author Chris Wolf
  7.  * @version 1.0.0 (September 1, 2019)
  8.  * @contact chriswolfdesign@gmail.com
  9.  */
  10. public class Driver {
  11.     /**
  12.      * main method
  13.      *
  14.      * @param args 0 -- the number of times to iterate through the for loop
  15.      *             1 -- the string we are printing out
  16.      */
  17.     public static void main(String[] args) {
  18.         // user must enter two command line arguments
  19.         if (args.length != 2) {
  20.             System.out.println("Usage: java Driver <iterations> <phrase>");
  21.             System.exit(1);
  22.         }
  23.  
  24.         // second command line argument must be an integer
  25.         if (!isNumeric(args[0])) {
  26.             System.out.println("ERROR -- iterations must be an integer");
  27.             System.exit(2);
  28.         }
  29.  
  30.         // third command line argument must not be empty
  31.         if (args[1].equals("") || args[1].equals(" ")) {
  32.             System.out.println("ERROR -- phrase must not be empty");
  33.             System.exit(3);
  34.         }
  35.  
  36.         // if all command line arguments are correct, continue
  37.         for (int i = 0; i < Integer.parseInt(args[0]); i++) {
  38.             System.out.println(args[1]);
  39.         }
  40.     }
  41.  
  42.     /**
  43.      * Checks to see if the string passed in a valid integer
  44.      *
  45.      * @param number the string we are checking
  46.      *              
  47.      * @return true if the string is numeric, false otherwise
  48.      */
  49.     private static boolean isNumeric(String number) {
  50.         try {
  51.             int num = Integer.parseInt(number);
  52.             return true;
  53.         } catch (NumberFormatException nfe) {
  54.             return false;
  55.         }
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top