Java- Reading Command Line Arguments

Reading Command Line Arguments

  1. Common way

     class ReadCmdLineArgs {
         public static void main(String[] args) {
             System.out.println("Command Line Args:");
             System.out.println("------------------");
             if (args.length != 0) {
                 for (int i = 0; i < args.length; i++) {
                     System.out.println((i + 1) + ". " + args[i]);
                 }
             } else {
                 System.out.println("No command line args. specified");
             }
         }
     }
    
  2. Another for loop syntax

    class ReadCmdLineArgs {
        public static void main(String[] args) {
            System.out.println("Command Line Args:");
            System.out.println("------------------");
            if (args.length != 0) {
                int i = 1;
                for (String arg: args) {
                    System.out.println((i++) + ". " + arg);
                }
            } else {
                System.out.println("No command line args. specified");
            }
        }
    }