Getting Input from Command Line Arguments


One way to provide input to the atmosphere modeling code is through command line arguments. If you recall, command line arguments are strings typed after the "java <classfile>" command to run a Java program. White space is the standard delimiter for command line arguments. Compound words can be treated as a single command line argument by enclosing them in double quotes.

Command line arguments are passed to the main() method as a String array, conventionally named args[] . The elements of the String array can be parsed as required and the arguments can then be used as needed. The advantage of command line arguments is the I/O code is easy to program. There are no menus to code or input files to read and parse. The I/O code is contained within the driver code. No additional files are required.

Using command line arguments is impractical if you have a large number of input arguments. The user also has to know the number, type, and order of the input arguments, although you can build in code to help the user if she makes a mistake. This method of I/O is also unforgiving of typographical errors. You have to type in all the arguments every time the code is run, and if you make one mistake you must repeat that process.

The CmdLineDemo class demonstrates how to acquire input data from command line arguments. The main() method of the CmdLineDemo class creates a USatm76 object using command line arguments to provide the necessary inputs to the USatm76 constructor. The first part of the main() method makes sure that the user provides two command line arguments. The first argument to the USatm76 constructor is a String indicating the system of units that will be used. The second argument to the constructor is a double value containing the altitude. Command line arguments are always read in as Strings . The second command line argument is converted from type String to type double . The USatm76 object is then created and the atmospheric conditions are written to standard output.

The CmdLineDemo class source code is shown here.

 public class CmdLineDemo {   public static void main(String args[]) {     //  Make sure the user provides two     //  command line arguments     if (args.length != 2) {       System.out.println(           "usage: java CmdLineDemo <units> <altitude>");       System.exit(1);     }     //  Convert the second command line argument     //  into a double     double altitude = Double.parseDouble(args[1]);     //  Create a USatm76 object with the recently     //  acquired input values.     USatm76 atm = new USatm76(args[0], altitude);     //  Print out the results to standard output     String label[] = atm.getLabels();     System.out.println("\ngeometric altitude    = " +                         atm.getAltitude() + label[0]);     System.out.println("geopotential altitude = " +               atm.getGeoPotentialAltitude() + label[1]);     System.out.println("temperature           = " +                        atm.getTemperature() + label[2]);     System.out.println("pressure              = " +                         atm.getPressure() + label[3]);     System.out.println("molar mass            = " +                         atm.getMolarMass() + label[4]);     System.out.println("density               = " +                         atm.getDensity() + label[5]);   } } 

Output ”

If the code is run using the syntax

 java CmdLineDemo SI 20000.0 

The resulting output will be ”

 geometric altitude    = 20000.0 m geopotential altitude = 19937.27227876952 m temperature           = 216.65 K pressure              = 5529.256361823237 N/m^2 molar mass            = 0.0289645 kg/mole density               = 0.08890932913275061 kg/m^3 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net