Pages

Monday, January 2, 2012

Command Line Arguments

Command Line Arguments
There are certain conditions where the user is required to pass arguments while running a program at the command line. For example, in DOS to delete a file we use the command “del filename” at the “C:” prompt and the specified file is deleted. In this case “del” is the command or the action that we want to perform and “filename” is known as the “Command Line Argument”. It is a value that is passed as input to a program during its call.
          In Java, all the command line arguments are stored automatically in an array named “args[]” declared in the main(). When we pass a value at the command prompt it automatically get stored in the “args” array. Let’s see how arguments aare passed and processed in a Java program.

  • Create the following program
class prog3
{       
          public static void main(String args[])
          {
                   System.out.println(“Hello “ + args[0]);
          }
}
  • Save and compile the program as
     
    javac prog3.java
  • Run the program using the “java” interpreter and pass the command line arguments
     
    java prog3 Steve
  • The output of the program will be
     
    Hello Steve.
How does it work ?
This is quite simple, the value passed at the command prompt (Steve) is the command line argument. Java stores the value in a predefined array named “args”. Since only one value has been passed by the user, it is stored in the index no “0” of the array. The “System.out.println()” statement prints the value of the 0 index along with the “Hello” message. As I told earlier, you can change the name “args” to any other valid name.

Passing more the one arguments 
Let’s make another program that takes multiple values as command line arguments
  •  Create the following program
     
    class prog4
     
    {
     
            public static void main(String args[])
    {
     
                   String str1,str2;
                   Str1=args[0];
                   Str2=args[1];
                   System.out.println(“Hello ” + str1+ “ “ + str2);
          }
}
 
  • Save and compile the program as
     
    javac prog4.java
  • Run the program using the “java” interpreter and pass the command line arguments
     
    java prog4 Steve Jobs
The output this time will be
Hello Steve Jobs
 The Java compiler will store the first argument in the index 0 of the args array and the second argument in the index no 1. The “println()” statement is simply printing it along with the message “Hello”
Passing integer values as command line arguments
Now the question is, Since “args” is a String array will it be able to handle integer values? Or  Can I pass numerical values as command line arguments also? The answer to both the questions is YES.  As you know strings are capable of storing any type of value and numericals are no exception, it can also store the numerical values but before performing any arithmetical calculation to these values we will have to convert them to integer or double.
Take a look
class prog4
{
          public static void main(String args[])
          {
                   int a,b,c;
                   a=args[0];
                   b=args[1];
                   c=a+b;
                   System.out.println(“Sum is “ + c);
          }
}
  • javac prog4.java
  • java prog4 10 20
In the line “a=args[0]”, we are copying a string value (args[0]) to an integer variable (a), since this is not allowed, the program will return an error. To copy the value of a string to an integer, it must be first converted to integer using the function “parseInt”. Following is the corrected version of the same program where the values have been converted to integers before assigning their values to the integer variables.
 class prog4
{
          public static void main(String args[])
          {
                   int a,b,c;
                   a=Integer.parseInt(args[0]);
                   b= Integer.parseInt(args[1]);
                   c=a+b;
                   System.out.println(“Sum is “ + c);
          }
}
  • javac prog4.java
  •  
  • Java prog4 10 20
When this program is compiled you will the result 30 will be printed. The function “parseInt” is used to convert a string value to its integer equilvalent. The string values are converted into integer and copied to integer values. To convert a string to a double value use “Double.parseDouble” instead.

0 comments:

Post a Comment