Command Line
Arguments
Command-line arguments: strings that follow the name of the
application program on the command line when the program is executed.
These command line arguments are Strings which are passed as arguments to your program's main method, via the parameter:
public static void main(String[] args)
If you are accustomed to interacting with computers through graphical
user interfaces, say by using Mac OS or Windows, then the term "command
line" may not have any meaning to you. In a unix environment, a console
window can be used to enter commands by just typing them, at what is
called a command line prompt:
>> cp file1.txt file2.txt
>> rm incriminatingEvidence.txt
>>
In these examples, cp and rm are the commands, and the .txt files are the arguments that we are passing to the programs that carry out those commands.
cp stands for copy, and rm stands for remove.
In the first command above, I am making a copy of file1.txt, called file2.txt. In the second command, I am removing a file called incriminatingEvidence.txt from the current directory.
Java Programs that use Command Line Arguments
Command line arguments appear on the command line, after the
program/command name. The first such value is stored in args[0], where
args is the main method's parameter. The second value is stored in
args[1], and so on.
Example: Suppose you have a
program called FileCopy that copies one file to
another. To compile this program, you might type the following command:
>> javac FileCopy.java
To run this program, you then type:
>> java FileCopy oldFile.txt newFile.txt
^
^
args[0] args[1]
Example: Program that takes 1 or more words on the command line, and prints them to the console
public class CommandPrinting {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
Sample compilation and execution of CommandPrinting, from a command line prompt:
>> javac CommandPrinting.java
>> java CommandPrinting Hello, world! Elvis lives.
Hello,
world!
Elvis
lives.
>>
In BlueJ: When we execute the main() method of our program, we include
our command line arguments inside double quotes, and the list of
command line arguments should be enclosed in braces. For example,
{"oldFile.txt", "newFile.txt"}.
Example: Write a program that
takes some number of Strings on the command line, and prints them in reverse order to the console.
Converting Strings to Numeric
Types
We would like to be able to take a sequence of integers or floating point
numbers on the command line, and perform arithmetic operations on these
numbers. However, the command line arguments are stored as strings. So
we want to convert, or parse, Strings to an int or double (or other
numeric type).
In the Integer class: parseInt(String s)
-
parses string s as an int, and returns
the int version
Example:
String myNumAsString =
"3";
int myNum = Integer.parseInt(myNumAsString); // myNum is 3
myNum++; // now myNum is 4
Exercise: Write a program that
takes some number of integers on the command line, and prints their
sum. If there are no command line arguments, print an error message.
There are similar parsing methods for other numeric types:
In the Double class: parseDouble(String s)
In the Float class: parseFloat(String s)
Example: String s = "3.14159";
double d = Double.parseDouble(s); // so d is 3.14159