The purpose of the sample program below is to demonstrate how to pass
in arguments to a Java application and verify the arguments you expect to
do work with have in fact been passed in. The program below will count
each argument and increment a counter which ultimately presents the total
number of arguments. If no arguments are passed in, the program will end
and not let the program's execution go any farther.
Proper way to write code for use of arguments:
public class Arguments {
public static void main(String[] args)
{
int total = 0;
if (args.length > 0)
{
for (int i = 0; i < args.length; i++)
{
total++;
}
System.out.println("Total number of arguments is: " +
total);
}
else
{
System.out.println("Forgot to put in arguments");
return;
}
}
}
When running the program above, I passed the values " 0 1 91" as
arguments.
OUTPUT:
Total number of arguments is: 3
Code Explanation:
The class name is declared and the name of the .Java file must be the
exact same name as the class name.
Next, you have the entry point to the program which is signified by the
line public static void main. This line is a requirement as an entry point
for all Java programs.
You then have an integer variable called total that is defined and
initialized to a value of zero
Next is an if statement that uses the args object length method to see
if there are any array elements in the args object.
In the event there are array elements in the object called args, the
for loop will loop once per argument and increment a counter. Once the
looping is done, the counter or total number of arguments is
displayed.
Now, you see an else statement that is in the code. This else statement
is part of the if statement that looked to see if there are any arguments
contained within the array that is input to the main. If there are no
arguments, this will cause the program to end rather than going on further
in its execution.
At this point, you may ask why - a return in the program within an else
statement is missing and the program is allowed to continue running. Look
at the statement below:
if (args[0] == "Hello")
{
System.out.println(args[1]);
}
This functionality under the condition that there are arguments passed
in to the main, will be evaluated. If there are no arguments to evaluate,
What will happen then?
public class Arguments {
public static void main(String[] args)
{
int total = 0;
if (args.length > 0)
{
for (int i = 0; i < args.length; i++)
{
total++;
}
System.out.println("Total number of arguments is: " + total);
}
if (args[0] == "Hello")
{
System.out.println(args[1]);
}
}
}
Pass in the arguments "4 3 1" and run the code.
OUTPUT:
Total number of arguments is: 3
NOW, remove all arguments and run the same program.
OUTPUT:
java.lang.ArrayIndexOutOfBoundsException: 0
at Arguments.main(Arguments.java:35)
Exception in thread "main"
Lesson summary:
What you have learned here is that when you pass in arguments and you
expect to do work on those objects, you must make sure you write the code
to handle the situation where someone forgets to pass in the arguments to
run the code.
NOTE This type of checking should not only be done in the main
but in any piece of java that will be expecting an argument.
You have also seen one way that the error
ArrayIndexOutOfBoundsException occurs. Remember this little example going
forward and know in the future why these kinds of errors may occur.
NOTE These examples have been developed and tested in WebSphere
Application Developer version 5.1.
|