Handling Command-Line Arguments
Command-line arguments are parameters passed to the main()
method of your Java program. In each example so far, you've seen that the main()
method takes in an array of String
values. These are the command-line arguments to the program.
Command-line arguments prove their usefulness by giving you one way of providing inputs to your program. These inputs are part of the command line launching the program, when run from a Terminal shell window.
Exercise 15: Testing Command-Line Arguments
This exercise shows how to pass command-line arguments to a Java program, and also shows how to access those arguments from within your programs:
- Using the techniques from the previous exercises, create a new class named
Exercise15
. - Enter the following code:
public class Exercise15 { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { ...