Logout

Input Using the args Array

There are two things that make computers so powerful. One is that they can do simple (and not so simple) things over and over again, thousands, and millions of times. But the other thing that makes computers powerful is their ability to function differently depending on information input into them. There are three general ways we can input information into our Netbeans programs so that they take advantage of this capability:

1. Graphical User Interface (GUI)
2. Console input
3. Using the "args" array parameter of the main method.

Graphical User Interface (GUI) is the option you most often use. This is the windows, buttons, and menus interface which you are familiar with just about all applications you use. Whereas console-based input is the old fashioned kind which you may have seen on TV, where the user types text commands on a black screen to operate it.

The args array is also a rather old fashioned way to get information into our programs. This occurs entirely within Netbeans, so you can't use it for an application you want to distribute to other people. In the project Properties window, you locate the Run tab, and enter information separated by commas. This information can be accessed by using arg[n] where n is the number of the piece of information you want to use, only with the numbering starting at 0.

So if I set the args array to be:

[0]     [1]    [2]    [3]
dog, cat, horse, cow

Then if I had the following statement:

System.out.println(args[2]);

I would get on the console output:

horse

 

* Note that with Netbeans version 6.9.1 this didn't seem to work.