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..)
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..)
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(s s));
}
}
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.
}
}
do you mean
public static void main(String[] args)
{
if (args != null && args.length == 3)
{
Order order = new Order();
order.setQuantity(Integer. parseInt(a rgs[0]));
order.setItem(args[1]));
order.setPrice(Double.pars eDouble(ar gs[2]));
}
}
given the input you've described
public static void main(String[] args)
{
if (args != null && args.length == 3)
{
Order order = new Order();
order.setQuantity(Integer.
order.setItem(args[1]));
order.setPrice(Double.pars
}
}
given the input you've described
ah, sorry, didn't refresh before posting.
ASKER
Sorry CEHJ.. the input is supposed to be specified in the format i have mentioned.. :(
ASKER
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
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
ASKER
yes CEHJ. i am expecting seperate lines..
I'd use a file. Can you do that?
ASKER
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?
ASKER
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?
ASKER
well folks.. ill try with a file then .. cud u help me out if the inputs were in a file..
ASKER
CEHJ.. u said u wud prefer a file.. can i hve thath soln please..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You may want to check, commons-cli (Its generic library for parsing command line arguments)
http://jakarta.apache.org/commons/cli
http://jakarta.apache.org/commons/cli
:-)
CD,1,10.50 phone,2,500 book,1,200
etc?