Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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..)
0
royalcyber
Asked:
royalcyber
  • 7
  • 7
  • 4
  • +1
1 Solution
 
CEHJCommented:
How about

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

etc?
0
 
CEHJCommented:
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));
            }
            
  }
0
 
SirCroftyCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
SirCroftyCommented:
ah, sorry, didn't refresh before posting.
0
 
royalcyberAuthor Commented:
Sorry CEHJ.. the input is supposed to  be specified in the format i have mentioned.. :(  
0
 
royalcyberAuthor Commented:
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..
0
 
SirCroftyCommented:
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

0
 
CEHJCommented:
You're not expecting separate lines too are you?
0
 
CEHJCommented:
You would be better off providing a file for input i think
0
 
royalcyberAuthor Commented:
yes CEHJ.  i am expecting seperate lines..
0
 
CEHJCommented:
I'd use a file. Can you do that?
0
 
royalcyberAuthor Commented:
well.. no CEHJ.. sorry.. this is my spec .. :(
0
 
SirCroftyCommented:
Does it have to be command line? I agree, the separate lines specification is not ideal. There is no front end possible?
0
 
royalcyberAuthor Commented:
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?
0
 
royalcyberAuthor Commented:
well folks.. ill try with a file then .. cud u help me out if the inputs were in a file..
0
 
royalcyberAuthor Commented:
CEHJ.. u said u wud prefer a file.. can i hve thath soln please..
0
 
CEHJCommented:
Read the file:

http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html

process could be

private List<String[]> args = new ArrayList<String[]>();

...

private void process(String line) {
    args.add(line.split(" ")); // ignore 3rd arg
}
0
 
Ajay-SinghCommented:
You may want to check, commons-cli (Its generic library for parsing command line arguments)
http://jakarta.apache.org/commons/cli
0
 
CEHJCommented:
:-)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 7
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now