Results 1 to 3 of 3
- 03-04-2011, 08:19 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 22
- Rep Power
- 0
Command line args for your java app!
Greetings everyone! I just posted my first post here on the intro forum yesterday, and am a fairly new (in my first year) java enthusiast and student. I find it extremely helpful for me to write myself a tutorial for everything i learn, even if its opening a simple 6 line text file in vim just to jot a reminder note. This helps me then in turn to convert what i just learned to permanent knowledge.
I have also noticed that because theres just plain a LOT of things you can do with Java , finding the 'perfect' tutorial or example for what you want to do can be tough sometimes. I had this problem with figureing out how to add command line arguments to my simple io apps. Using if statements i figured out how to make an app have arguments, but it was terribly ugly and the app could only run with as many arugments as i took the time to write a seperate block of if statements for. Finally i got the idea of a for loop and it works great! The prog can have as many arguments as the user wants to put in, if one isnt valid it will tell u and move on to the next. This is how i did it, and i hope this helps someone!! And if theres a better way to do this someone plz let me know :D
Java Code:/* * Java application designed to demonstrate simple arguments * and show the ability to accept as many arguments as the user * decides to input, independent of weather or not they are valid */ public class argDemo { public static void main(String[] argv) { if (argv.length > 0) { System.out.println("This program has arguments"); for (int i=0; i < argv.length; i++){ if (argv[i].equals("-v")) { System.out.println("JavArg Demo v1.0"); } else if (argv[i].equals("-a")) { System.out.println("All Possible Arguments: -v, -a, -x, -d"); } else if (argv[i].equals("-x")) { System.out.println("You have chosen to exit the program early."); System.exit(0); } else if (argv[i].equals("-d")) { System.out.println("You have enabled debugging"); System.out.println("Creating log files.."); //write log files using filewriters wrapped in bufferedwriters } else { System.out.println(""+argv[i]+" is an invalid argument"); } } //End of for loop } //End of arg if statment System.out.println("Thank you for using JavArg Demo"); } }
- 03-04-2011, 08:54 PM #2
Another way to do this is to simply form one large String out of the arguments and just check whether each is a substring of that large String.
Or you could convert the array to a List and use the contains() method.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-04-2011, 09:05 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
How do you add command line args?
By AcousticBruce in forum IntelliJ IDEAReplies: 6Last Post: 12-16-2010, 04:51 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
command line args
By MarkWilson in forum NetBeansReplies: 3Last Post: 08-04-2008, 03:22 AM -
Java command line
By agouzoul in forum New To JavaReplies: 2Last Post: 04-02-2008, 12:12 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks