Results 1 to 2 of 2
  1. #1
    vipvan2000 is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Default Parsing Argument Values

    Java Gurus,
    Need some help here

    We use command line scripts to pass values like

    java submit <input_file=test1> <output_dir=dir_name>

    I have a need to pass the argument values to another program. How do I parse these values.
    Sorry if it was asked earlier but I am a little new to Java.

    Thanks in advance

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.util.Arrays;
    
    public class Test {
        public static void main(String[] args) {
            // <input_file=test1> <output_dir=dir_name>
            // The vm didn't like a left bracket so I did this:
            // java Test "<input_file=test1> <output_dir=dir_name>"
            System.out.printf("args = %s%n", Arrays.toString(args));
            String inputPath = "", outputPath = "";
            String[] s = args[0].split("\\s");
            System.out.printf("s = %s%n", Arrays.toString(s));
            if(s.length == 2) {
                inputPath = parse(s[0]);
                outputPath = parse(s[1]);
            }
            System.out.println("inputPath  = " + inputPath);
            System.out.println("outputPath = " + outputPath);
        }
    
        private static String parse(String s) {
            int start = s.indexOf("=")+1;
            int end = s.indexOf(">");
            return s.substring(start, end);
        }
    }

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  3. How to resolve Constructor argument in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 12:43 PM
  4. Parsing Argument Values
    By vipvan2000 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 11:01 AM
  5. How to resolve Constructor argument in Spring
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •