Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Changing a program to accept command line arguments

  1. #1
    Junior Member coderpuff_girl's Avatar
    Join Date
    Sep 2021
    Location
    Bangalore
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Changing a program to accept command line arguments

    I have the following piece of code, I want to modify it so that it can accept command line arguments w/o using a scanner to read the file. What changes do I need to make in my code? I have a file called prgm.cmd, prgm.cmd is the actual argument! and I'll execute it on UNIX as follows:

    java Commander prgm.cmd

    The only way to make my program work currently is as below:

    java Commander < prgm.cmd

    Here is the CODE:
    import java.util.Scanner;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
     
    public class Commander
    {
        public static void main(String[] args)
        {
            Map<String,Integer> expression = new HashMap<String,Integer>();
            ArrayList<String> list = new ArrayList<String>();
     
            Scanner sc = new Scanner(System.in);
     
            while(true)
            {
                list.add(sc.nextLine());
                if(!sc.hasNextLine()) break;
            }
     
            ArrayList<String> tokens = new ArrayList<String>();
            ArrayList<String> PRINT = new ArrayList<String>();
     
     
            for(String element : list) {
                StringTokenizer st = new StringTokenizer(element);
     
                if(!element.startsWith("PRINT")) {
                    while(st.hasMoreTokens()) {
                        tokens.add(st.nextToken());
                    }
     
                    expression.put(tokens.get(0),Integer.parseInt(tokens.get(2)));
                    tokens.clear();
                } else {
                    while(st.hasMoreTokens())
                        PRINT.add(st.nextToken());
                    System.out.println(expression.get(PRINT.get(1)));
                    PRINT.clear();
                }
            }
        }
    }

    prgm.cmd command file

    A = 6   
    C = 14
    PRINT C 
    B = 12
    C = 8
    PRINT A

    Output
    14
     6

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,530
    Thanks
    55
    Thanked 2,659 Times in 2,609 Posts

    Default Re: Changing a program to accept command line arguments

    Command line arguments are passed to the main method's arguments in a String array.
    This command: java Commander prgm.cmd
    will set args to a single element String array with prgm.cmd in the first element: args[0]
    To see what the main method receives, print it out with this:
       System.out.println("args="+java.util.Arrays.toString(args));  // show args
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member amodnazirkar's Avatar
    Join Date
    Sep 2021
    Location
    Bangalore
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Changing a program to accept command line arguments

    public static void main(String[] args)
    //                      ^^^^^^^^^^^^^

    If you run your program like:
    java progname arg1 arg2

    The arguments will appear in a string array handed to main(). Just extract them from there and do what you need.

    The following program shows this in action. It will echo back your arguments, one per line:
    public class Test {
        public static void main(String[] args) {
            for (int i = 0; i < args.length; i++)
                System.out.println (args[i]);
        }
    }

    This is to get the commands as arguments to the program.

    If, instead, you want to still have the commands in a file and just supply the file name to the program, you simply need to change your scanner to use a file based reader rather than System.in

    The following program accepts a file name argument then echos it to the screen:

    import java.io.FileInputStream;
    import java.util.Scanner;
     
    public class Test {
        public static void main(String[] args) {
        try {
            Scanner sc = new Scanner (new FileInputStream(args[0]));
            while (sc.hasNextLine())
                System.out.println (sc.nextLine());
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }

    You can even make it selectable, like UNIX filter programs using - to indicate standard input.

    I hope this answered your query!

    Read https://www.scaler.com/topics/comman...ments-in-java/ an Informative resource on Command Line Arguments in Java.

Similar Threads

  1. Command Line and two arguments
    By SeanyC in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 14th, 2014, 09:28 PM
  2. Arrays , command line arguments
    By Jad_Asmar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 30th, 2013, 11:25 AM
  3. Help understanding Command-Line Arguments
    By Truck35 in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2012, 06:16 PM
  4. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM
  5. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM

Tags for this Thread