Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Driver.java
- *
- * The driver for our command line argument example
- *
- * @author Chris Wolf
- * @version 1.0.0 (September 1, 2019)
- * @contact chriswolfdesign@gmail.com
- */
- public class Driver {
- /**
- * main method
- *
- * @param args 0 -- the number of times to iterate through the for loop
- * 1 -- the string we are printing out
- */
- public static void main(String[] args) {
- // user must enter two command line arguments
- if (args.length != 2) {
- System.out.println("Usage: java Driver <iterations> <phrase>");
- System.exit(1);
- }
- // second command line argument must be an integer
- if (!isNumeric(args[0])) {
- System.out.println("ERROR -- iterations must be an integer");
- System.exit(2);
- }
- // third command line argument must not be empty
- if (args[1].equals("") || args[1].equals(" ")) {
- System.out.println("ERROR -- phrase must not be empty");
- System.exit(3);
- }
- // if all command line arguments are correct, continue
- for (int i = 0; i < Integer.parseInt(args[0]); i++) {
- System.out.println(args[1]);
- }
- }
- /**
- * Checks to see if the string passed in a valid integer
- *
- * @param number the string we are checking
- *
- * @return true if the string is numeric, false otherwise
- */
- private static boolean isNumeric(String number) {
- try {
- int num = Integer.parseInt(number);
- return true;
- } catch (NumberFormatException nfe) {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement