Results 1 to 3 of 3
  1. #1
    aadem is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default 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.");
    			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");

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power


    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 - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    aadem is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default ahhh!

    why your absolutely right! i love these forums already im documenting those 2 methods right now it my little scratchbook haha! Thanks again i needed some homework on substrings anyhow, and need to investigate more into what methods are hidden down depe in the array datatype for my use

Similar Threads

  1. How do you add command line args?
    By AcousticBruce in forum IntelliJ IDEA
    Replies: 6
    Last Post: 12-16-2010, 05:51 PM
  2. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  3. command line args
    By MarkWilson in forum NetBeans
    Replies: 3
    Last Post: 08-04-2008, 03:22 AM
  4. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts