Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,125 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,076 people online right now. Registration is fast and FREE... Join Now!




Command-line Arguments Tutorial

 
Reply to this topicStart new topic

> Command-line Arguments Tutorial

AntonWebsters
Group Icon



post 17 Sep, 2009 - 11:30 AM
Post #1


Hi there! I'm Anton, and I'll be teaching all of you readers about Command-Line Arguments.

Now, every time you start to code in Java, there MUST be a main method, right? Have you ever wondered what does the "String[] args" parameter do?

CODE

public static void main(String[] args){
    // Codes here...
}


The main method is just like a regular method with a parameter, so of course it's possible to pass arguments to main method.

Assuming that the name of the program is JavaExample, this is what Java coders type to run their programs via command prompt(Windows) or terminal(Linux):

CODE

java JavaExample


Consider this example:
CODE

java JavaExample "I Love DIC" Hola 500


In this example, three strings are passed to the main method when the program runs.
Don't be fooled by the "500"...it's not an integer, it's actually treated as String.
Since all the args entered are considered as Strings, there is no need to use double-quotes, like what I did with I Love DIC.

I am going to show you how it works in a complete program now.

CODE


public class TestCommandLineArgs {

    /**
     * @param args
     */
    public static void main(String[] args) {
        if(args.length != 3){
            System.err.println("Usage: java TestCommandLineArgs numbers1 numbers2 numbers3");
            System.exit(0);
        }
        
        int num1 = Integer.parseInt(args[0]);
        int num2 = Integer.parseInt(args[1]);
        int num3 = Integer.parseInt(args[2]);
        
        int sum = num1 + num2 + num3;
        System.out.println("The sum of the three numbers are " + sum);
        
    }

}


This program is used to sum up all three numbers that are entered as args.

The if(args.length != 3) statement is used to ensure that the user has entered 3 arguments. The program terminates if the user enters less than or more than 3 arguments.

Since the args entered are of String type, we need Integer.parseInt(args[0]) to convert them to integer.
Then calculation's made, and the sum is printed out.

I hope that my explanations and example on Command-Line arguments are clear enough for all of you guys.
Thanks for reading. =)


Attached thumbnail(s)
Attached Image
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

NeoTifa
Group Icon



post 5 Oct, 2009 - 08:37 AM
Post #2
C:/Hacking Tools? >_> very nice though.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 02:23PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month