Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: newbie start java program with switches

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default newbie start java program with switches

    Hi Guys,

    Im working on an assignment for my school and for example i need to be able to excute my program from DOS adding switches in for example.

    java Main -e password input.txt output.text
    or
    java Main -d password input.txt output.text

    so depending on the switches the program will run differntly

    The program i am creating(Main.java) will encrypt(-e) or decrpt(-d) a text file based on the password(password). it will have an input text file (input.txt) with infomation in clear text and an output text file which will have the infomation encrypted (output.text).

    Im very rusty with my java programming and i have a lot to learn and catch up but i dont know how i can achive this can you please help me.

    I am using netbeans to create my program with


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: newbie start java program with switches

    please can someone point me in the right direction

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: newbie start java program with switches

    You can do it like this:

    public class ProgramArguments {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		if(args.length < 1){
    			System.out.println("No argument");
    		}		
    		else if(args[0].equals("-d")){
    			System.out.println("Decrypt Method");
    		}
    		else if (args[0].equals("-e")){
    			System.out.println("Encrypt Method");
    		}
    	}
    }

    This is just an example.

    java -jar ProgramArguments.jar -d

     

    Decrypt Method




    args[1] password
    args[2] input.txt
    args[3] output.txt
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: newbie start java program with switches

    ok say for instance i wanted to check if the password part of the starting argument was happy would i look in args[1] for example
     if (args[1].equals("password1234")){
     
    veriffy password ect ect 
     
    }

    becasue i need to run the password through an algorythum to verify if it is correct or not



    and the text files would be ars [2] abd args[3]


    also do you know how i can use netbeans to run the java application wth the arguments ???

    EDIT: I FOUND OUT HOW Netbeans IDE Blog by Tushar Joshi, Nagpur: Using Command Line Arguments in NetBeans IDE
    Last edited by fortune2k; November 1st, 2010 at 04:01 PM.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: newbie start java program with switches

    Yes args[1] would be the password string

    Glad you found out how to run command line arguments with Netbeans.

    If you ever use Eclipse, you can do the same thing like this:

    http://www.javaprogrammingforums.com...s-eclipse.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Start With JAVA
    By infoprovider in forum The Cafe
    Replies: 2
    Last Post: July 28th, 2010, 04:34 AM
  2. Newbie needs help in simple servlet program
    By visitor1078 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 19th, 2009, 01:46 PM
  3. switches and else if
    By silverspoon34 in forum Collections and Generics
    Replies: 1
    Last Post: November 11th, 2009, 04:09 PM
  4. How should i write and compile java with Ubuntu?
    By Talk Binary in forum Java IDEs
    Replies: 19
    Last Post: May 7th, 2009, 05:29 AM
  5. Best way to learn java for beginners
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: May 8th, 2008, 04:37 AM