user2967784
user2967784

Reputation: 33

Process command line arguments

We have a shell script which invokes my Java program. The script passes arguments values for methods defined in main method of my Java program.

The expected input format for parameters is as follows

"Test1, Test1_21APR15,XYZ,Test,Test, , , 2015-04-21"  
"Test2, Test2_21APR15,XYZ,Test,Test, , ,2015-04-21"   
"Test3,Test3_21APR15,XYZ, Test,Test, , ,2015-04-21"

and so on, i.e. each string has attributes which are comma separated and string are separated by a space (here I have mentioned in the next line but actual values will be separated by space).

From the above input values I need to assign values to local attributes as shown below:

attr1 = Test1,Test2,Test3   
attr2 = Test1_21APR15,Test2_21APR15,Test3_21APR15  
attr3 = XYZ,XYZ,XYZ  
attr4 = Test,Test,Test   
.  
.  
.  
attr8 = 2015-04-21,2015-04-21,2015-04-21,

and then I need to process these parameters in my methods.

I understand that when you pass arguments to the main method they are placed in arg[] array, but now I am facing issues while assigning the parameters values to attributes.

Can someone please give me some guidance me on this? Thanks in advance.

Upvotes: 2

Views: 2862

Answers (5)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

public class YourClass {
    public static void main (String[] args) {
        if (args == null) {
            System.out.println("No arguments supplied, exiting");
            System.exit(0);
        }
        // length() is wrong
        int numAttr = args[0].split(",").length;

        String[] attr = new String[numAttr];

        for (int i=0; i < args.length; ++i) {
            String[] parts = args[i].split(",");
            for (int j=0; j < parts.length; ++j) {
                if (i > 0) {
                    attr[j] += ",";
                }
                attr[j] += parts[j];
            }
        }
    }
}

You can iterate over the attr array to extract out the various values you need:

for (int i=0; i < attr.length; ++i) {
    System.out.println("attr" + (i+1) + "= " + attr[i]);
}

Output

attr1 = Test1,Test2,Test3
attr2 = Test1_21APR15,Test2_21APR15,Test3_21APR15
attr3 = XYZ,XYZ,XYZ
attr4 = Test,Test,Test
.
.
.
attr8 = 2015-04-21,2015-04-21,2015-04-21

Upvotes: 0

JimHawkins
JimHawkins

Reputation: 4998

It appears to me that the number of comma separated values within each args-string is forced by a data model.

So I suggest the solution below. It's not as short or generic as the other proposals, but maybe it matches your requirement.

package stackoverflow;

import java.util.*;

public class DemoMain
{
    final static int COUNT_COLUMNS =8;

    public static void main(String[] args) throws IllegalFormatException
    {
        StringBuilder[] attributes = new StringBuilder[COUNT_COLUMNS];
        for (int i=0; i< COUNT_COLUMNS; i++)
        {
            attributes[i]=new StringBuilder();
        }

        for (int numArgs=0; numArgs<args.length; numArgs++)
        {
            String[] values = args[numArgs].split(" *, *");
            if (values.length != COUNT_COLUMNS)
            {
                throw new IllegalArgumentException("invalid number of fields, must be "+ COUNT_COLUMNS);
            }

            int numVal=0;
            for (String val : values)
            {
                attributes[numVal].append(numArgs > 0 ? "," : "").append(val.trim());
                numVal++;
            }
        }

        for (StringBuilder att : attributes)
        {
            System.out.println(att);
        }
    }
}

Upvotes: 0

HJK
HJK

Reputation: 1382

If you pass each set as "," separated instead of space separated like

"Test1, Test1_21APR15,XYZ,Test,Test, , , 2015-04-21, Test2, Test2_21APR15,XYZ,Test,Test, , ,2015-04-21"

Then we could use following logic Here "testnum" is number of input lines we are referring and "testlen" is number of strings in each line.

for(int i=0;i<testlen;i++){
 for(int j=0;i<testnum;j++){   
  attr[i]=attr[i]+args[j*testlen+i]
  if(j<testnum-1)
     attr[i]=attr[i]+",";
 }
} 

Upvotes: 1

Don Mathew
Don Mathew

Reputation: 166

The parameters can be passed as :

java CommadLineArguments "parm1, param2" "param3, param4" ...

Here space is the delimiter for the jvm. It will split with space as delimeter and fill the args array.

The class will be :

public class CommadLineArguments {

       public static void main(String[] args) {
            if (args == null) {
                System.out.println(" No Arguments ");
            } else {
               String att1 = args[0];
               String att2 = args[1];
            }
        }

    }

Upvotes: 0

Sachin Gupta
Sachin Gupta

Reputation: 8368

You can pass command line arguments to a JAVA main class like this:

java TestMain "Test1, Test1_21APR15,XYZ,Test,Test, , , 2015-04-21" "Test2, Test2_21APR15,XYZ,Test,Test, , ,2015-04-21"  "Test3,Test3_21APR15,XYZ, Test,Test, , ,2015-04-21"

Here content of TestMain class would be

public class TestMain {    
    public static void main(String[] args) {            
        String attr0=args[0];
        String attr1=args[1];
        String attr2=args[2];

        ......................use attributes here..............         
    }    
}

Upvotes: 0

Related Questions