Link to home
Start Free TrialLog in
Avatar of royalcyber
royalcyber

asked on

Optimal Design to parse Command Line Arguments !

Hi experts,
                 The input spec for my program will be similar to the following :

                 1 CD at 10.50
                 2 phones at 500
                 1 book at 200

                 This input is given in this format in the command line.
                 [<quantity> <item> at <price>]

                 Can somebody give me a small piece of code to parse this kind of input from the command line   and set them into individual objects(say an object like Order which has separate setter methods for quantity,item and price..)
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

How about

CD,1,10.50 phone,2,500 book,1,200

etc?
Then

      public static void main(String[] args) throws Exception {
            List<String> _args = Arrays.asList(args);
            List<String[]> csvArgs = new ArrayList<String[]>();
            for (String s : _args) {
                  csvArgs.add(s.split(","));
            }
            for (String[] ss : csvArgs) {
                  System.out.println(Arrays.toString(ss));
            }
            
  }
do you mean

public static void main(String[] args)
{
    if (args != null && args.length == 3)
    {
        Order order = new Order();
        order.setQuantity(Integer.parseInt(args[0]));
        order.setItem(args[1]));
        order.setPrice(Double.parseDouble(args[2]));
     }
}

given the input you've described
ah, sorry, didn't refresh before posting.
Avatar of royalcyber
royalcyber

ASKER

Sorry CEHJ.. the input is supposed to  be specified in the format i have mentioned.. :(  
SirCrofty, but u r code works only if there is a single line of input rt..  i have a min of 3 .. :( n each on a different line..
Ah, sorry, I misinterpreted the question. You want something like System.in I believe. See the following:

http://www.devdaily.com/java/edu/pj/pj010005/pj010005.shtml

You're not expecting separate lines too are you?
You would be better off providing a file for input i think
yes CEHJ.  i am expecting seperate lines..
I'd use a file. Can you do that?
well.. no CEHJ.. sorry.. this is my spec .. :(
Does it have to be command line? I agree, the separate lines specification is not ideal. There is no front end possible?
well sirCrofty . the specs given to me are as such.. if i cud change it, i wud.. sorry :( . well, i cud even have more than 3 lines.. so is it possible to have a way specify the end of lines (say some character) and parse each line individually and then prob parse each line (using tokens or something similar) and then populate the objects?
well folks.. ill try with a file then .. cud u help me out if the inputs were in a file..
CEHJ.. u said u wud prefer a file.. can i hve thath soln please..
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may want to check, commons-cli (Its generic library for parsing command line arguments)
http://jakarta.apache.org/commons/cli
:-)