• Taking in command line args and parsing them with a String[] and Stack (Java)
    9 replies, posted
I'm working on this java class for my Comp Sci class (two different types of classes, heh). Anyway, I want to figure this out for myself, but I'm having a hard time starting off, trying to set whatever arguments are input to a String[], but I'm not sure of the command to do this. Take a look at the directions, then take a look at my code: [code]Directions: "In main( ), you will take in a sequence of parens as command line arguments. You will then use a Stack to verify whether or not that parens are legally paired. Primarily this will involve pushing closed parens, and then popping on open parens, or pushing open parens and then popping on closed parens, depending on how you are reading the string. Hint: Use the charAt(int index) method from the String implementation in the API to &#8220;parse&#8221; the string." What I have so far: -------------------------------- import java.util.Stack; public class ParenMatch { public static void main(String args[]) { Stack<String[]> stack = new Stack<String[]>(); for(String[] s: args) //this isn't phrased right. How would I do this? } }[/code] I'm assuming an enhanced for loop will just go through the list of arguments, which would be a series of open and closed parentheses, and set each of them to an index in the string array. But I'm not even sure this would be the best way of doing it. I think what I need to do is set the arguments (the sets of parentheses) to a String[], then go through the array and push or pop them to the Stack depending on what they are. I'm assuming at most I'll have two objects in a stack, since I am just testing to see if they are legally paired. I just need some hints or guidance on how to do this. Thanks to anyone in advance. [editline]06:01PM[/editline] Ah, I think I might be going about reading the command line arguments from the user all wrong. Can anyone clarify that this is the correct way of doing it? [url]http://www.devdaily.com/java/edu/pj/pj010005[/url] I'm assuming I could use the Scanner class instead of BufferedReader to take input. When my professor says "command line input," in my head I'm thinking: java Echo whatever .
That args-parameter of main is the command line [b]parameter[/b]. Command line [b]input[/b] would be any input passed via the command line, but I'd guess that your teacher refers to input from the console while the program runs, so yeah, you can simply use the java.util.Scanner for that. But to fix that parameter-parsing for you, you'd simply do for(String s: args) and wouldn't use a java.util.Stack at all. ;)
You wouldn't have a Stack of String arrays. Stack<String> would be fine. Heres how you convert that String[] into a Stack [code] Stack<String> s = new Stack<String>(); for(int i = 0; i < args.length; i++) s.push(args[i]); [/code] [editline]05:35PM[/editline] Like Zeeky said, you can do for(String s : args) But I never use enhanced for loops, so for me it'd look like this: [code] for(int i = 0; i < args.length; i++) { String s = args[i]; //do stuff } [/code]
[QUOTE=PvtCupcakes;17643014]Heres how you convert that String[] into a Stack [code] Stack<String> s = new Stack<String>(); for(int i = 0; i < args.length; i++) s.push(args[i]); [/code][/QUOTE] You need to fill the Stack backwards, if you want the first item to be processed first as well.
[QUOTE=ZeekyHBomb;17644314]You need to fill the Stack backwards, if you want the first item to be processed first as well.[/QUOTE] Or just use a queue which makes more sense in this situation
[QUOTE=ZeekyHBomb;17644314]You need to fill the Stack backwards, if you want the first item to be processed first as well.[/QUOTE] This is true. [editline]08:31PM[/editline] New code (I know it's not much more, I took a break): [code] public static void main(String args[]) { Stack<String> s= new Stack<String>(); for(int i = 0; i < args.length; i++) s.push(args[i]); while(s.empty() == false){ } } [/code] I'm having trouble understanding the directions when they say "Use the charAt(int index) method from the String implementation in the API to “parse” the string." Can anyone explain?
[cpp]public static void main( String[] args ) { Stack< String > s = new Stack< String>(); s.addAll( Arrays.asList( args ) ); // other shitz }[/cpp] :eng101:
[QUOTE=tjl;17652851][cpp]public static void main( String[] args ) { Stack< String > s = new Stack< String>(); s.addAll( Arrays.asList( args ) ); // other shitz }[/cpp] :eng101:[/QUOTE] Shorter code isn't always faster/more efficient code. This way it converts the array into a List and then in a Stack; is that really faster than directly converting it into a Stack?
Yes, it is probably slower. I just looked at the source, and Arrays.asList just returns an ArrayList of the passed args, and the addAll method is just this: [cpp] public synchronized boolean addAll(Collection c) { modCount++; Object[] a = c.toArray(); int numNew = a.length; ensureCapacityHelper(elementCount + numNew); System.arraycopy(a, 0, elementData, elementCount, numNew); elementCount += numNew; return numNew != 0; } [/cpp] Won't be nearly noticeable though. Behind the scenes, it's all backed by arrays, so it's usually very fast to insert elements (as long as you have enough space reserved).
System.arraycopy, huh? Couldn't you do [cpp]s.ensureCapacity(args.length()); System.arraycopy(args, 0, s, 0, args.length());[/cpp]
Sorry, you need to Log In to post a reply to this thread.