Command Line Arguments and Arrays

5.7.2 Command Line Arguments and Arrays

Our Newton's Method Java program allows the user to specify the initial guess, in this case 2, on the command line like this:
(maxwell@gamba.130) java Newton 2
Step: 1 x:4.18504 Value:-0.864144
Step: 2 x:2.46789 Value:0.623881
Step: 3 x:3.26619 Value:-0.124272
Step: 4 x:3.14094 Value:0.000648741
Step: 5 x:3.14159 Value:-9.10111e-11
Zero found at x=3.14159

The number 2 plays the role of a command line parameter. The arguments to the main method provide a mechanism for telling a Java program what the command line parameters are. You will remember that the main method has as its argument an array of Strings called argv. There are as many elements in the array as there are command line parameters, and our method checks to see if there are any such parameters. If you have an array called my_array, the length of the array is kept in the variable my_array.length. So, we can see if there are command line arguments by checking the length of argv.

Like any programming language, Java provides a way of testing values. In this case, it is the if construction:

	  if(argv.length==1) {
	    x= Double.valueOf(argv[0]).doubleValue();
Here, we test if there is exactly one command line argument. The test uses the boolean operator == which returns true if two numbers have the same numeric value, and false if they are different. Be sure not to confuse the boolean operator == with the assignment operator = - I make this mistake all the time! Some other useful boolean operators are: greater than (>), less than (<), and not equal (!=).

The keyword if takes either true or false as its argument. If its argument is true, the Java interpreter executes the commands in the following curly braces; otherwise it skips them.

So, if there is just one command line argument, we want to set our guess, x, to be it. But we have a problem! Firstly, this argument is nestled in the array argv, and secondly, it is of type String. We have to extract the value and convert it to type double.

Extracting the value is easy, but you have to remember a trick. Arrays in Java a numbered starting from 0. To get at the first element of the array argv, you would use argv[0] and to get at the second value, you would use argv[1]. This convention will be a bit confusing at first, but you will be surprised when you gain more experience at how it can actually shorten your code and make it more elegant.

On the other hand, converting from type String to type double is a bit more cumbersome. Java has a class called Double that contains methods for dealing with doubles, including some conversion routines. The first thing we need then, is to create a Double that has the value of the String argv[1]. This is done by the Double method Double.valueOf. Thus, Double.valueOf(argv[0]) makes a string with the value of the first argument. Now we need to turn the Double into a double, which is done with the Double method Double.doubleValue. Put it all together and you get

	x = Double.valueOf(argv[0]).doubleValue();
If this is a bit confusing, don't worry. In fact, if you ever want to use this in you own code, just copy the bit from the Newton example code. However, the business with arrays should be easy to rememeber.
Next 5.7.3 Calculating in the Main Loop

David Maxwell, who is still writing this, would like to hear your comments and suggestions.