Java Reference
In-Depth Information
of your command-line arguments, and pass that array to the main() method on your
behalf.
The main() method in the example displays each argument that is passed. First,
the length of the array named args is tested to see whether it is greater than zero. If it
is, the method will loop through each of the arguments in the array by executing a for
loop, displaying each argument along the way. If there are no arguments passed, the
length of the args array will be zero, and a message indicating such will be printed.
Otherwise, you see a different message followed by a list of arguments.
Command-line interpreters recognize spaces and sometimes other characters as de-
limiters. It's generally safe to pass numeric values as arguments delimited by spaces
without bothering to enclose each value within quotes. However, you should get into
the habit of enclosing character-string arguments in double quotes, as shown in the fi-
nal solution example. Do that to eliminate any ambiguity over where each argument
begins and ends.
Note All arguments are seen by Java as character strings. If you pass numeric values
as parameters, they enter Java as character strings in human-readable form. You can
convert them into their appropriate numeric types using the conversion methods shown
in Recipe 1-5.
1-7. Accepting Input from the Keyboard
Problem
You are interested in writing a command-line or terminal application that will accept
user input from the keyboard.
Solution
Make use of the java.io.BufferedReader and
java.io.InputStreamReader classes to read keyboard entry and store it into
local variables. Listing 1-7 shows a program that will keep prompting for input until
you enter some characters that represent a valid value of type long .
 
Search WWH ::




Custom Search