Java Reference
In-Depth Information
It is entirely up to the program what to do with the command line argument strings.
It is customary to interpret strings starting with a hyphen (-) as options and other
strings as file names. For example, we may want to enhance the LineNumberer
program so that a -c option places line numbers inside comment delimiters; for
example
java LineNumberer -c HelloWorld.java HelloWorld.txt
If the -c option is missing, the delimiters should not be included. Here is how the
main method can analyze the command line arguments:
for (String a : args)
{
if (a.startsWith("-")) // It's an option
{
if (a.equals("-c")) useCommentDelimiters =
true;
}
else if (inputFileName == null) inputFileName =
a;
else if (outputFileName == null) outputFileName
= a;
}
Should you support command line interfaces for your programs, or should you
instead supply a graphical user interface with file chooser dialog boxes? For a
casual and infrequent user, the grap hical user interface is much better. The user
interface guides the user along and makes it possible to navigate the application
without much knowledge. But for a frequent user, graphical user interfaces have a
major drawbackȌthey are hard to automate. If you need to process hundreds of
files every day, you could spend all your time typing file names into file chooser
dialog boxes. But it is not difficult to call a program multiple times automatically
with different command line arguments. Productivity Hint 7.1 discusses how to
use shell scripts (also called batch files) for this purpose.
502
503
11.2 Throwing Exceptions
There are two main aspects to exception handling: reporting and recovery. A major
challenge of error handling is that the point of reporting is usually far apart from the
point of recovery. For example, the get method of the ArrayList class may detect
Search WWH ::




Custom Search