Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

42 HOUR 4:Understanding How Java Programs Work


5: + “jumped over the “
6: + arguments[2] + “ dog.”
7: );
8: }
9: }

This application compiles successfully and can be run, but if you try it with
the menu command Run, Run File, you get a complicated-looking error:

Output▼
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at BlankFiller.main(BlankFiller.java:3)

This error occurs because the program expects to receive three arguments
when it is run. You can specify arguments by customizing the project in
NetBeans:


  1. Choose the menu command Run, Set Project Configuration,
    Customize. The Project Properties dialog opens.

  2. Enter BlankFillerin the Main Class text field.

  3. In the Arguments field, enter retromingent purple lactose-
    intolerantand click OK.


Because you’ve customized the project, you must run it a little differently.
Choose the menu command Run, Run Main Project. The application uses
the arguments you specified as adjectives to fill out a sentence, as shown in
the following output:

Output▼
The retromingent purple fox jumped over the lactose-intolerant dog.

Return to the Project Properties dialog and designate three adjectives of your
own choosing as arguments, making sure to always include at least three.
Arguments are a simple way to customize the behavior of a program. The
arguments are stored in a type of variable called an array. You learn about
arrays during Hour 9, “Storing Information withArrays.”

Creating an Applet
When the Java language wasintroduced, the language feature that got the
most attention was applets, Java programs that run on web pages. You can

LISTING 4.2 Continued
Free download pdf