Using Comand line arguments

The code below will process the String array that contains the command line arguments passed to the main method when Java runs.

A google search will show you how to turn on and provide command line args to your Java program in Eclipse, etc.

This example also show the "modes" your program needs to have. Mode 1 means you found no run args and so your main method spins up an interactive test driver to manually build and use a BST. Mode 2 means you did find run args, so you read those for the commands and data for building a BST non-interactively.



class RunArgsModesDemo {

  public static void main (String[] args) {
    if (args.length==0) {
      // there are no command like args
      System.out.println("mode 1");
      System.out.println("we will do the interactive test driver");
      // code after this for the loop reading a command from
      // the keyboard, etc.
      // loop until the q command
    }
    else {
      // there are command line args
      // so we will read the args one or two at a time 
      // from the String array args and do the commands 
      // requested .  
      String cmd;
      String assocData;
      System.out.println("mode 2");
      System.out.println("here are the args: \n");
      int na = args.length; 
      for (int i=0; i < na; i++) {
        cmd = args[i];
        System.out.println("command: "+cmd);
        switch (cmd) {
          case "i":
          case "r":
          case "c":
          case "g":
            assocData = args[++i];
            System.out.println("assoc: "+assocData);
        }
        System.out.println();
      }
      System.out.println();     
    }
  }
}