Hour 6. Using Strings To Communicate
Hour 6. Using Strings To Communicate
In the film The Piano, Holly Hunter portrays Ada, a young Scottish woman who marries badly. A
mute since the age of six, Ada can only express herself fully by playing her prized possession, a
piano.
Like Ada, your computer programs are capable of quietly doing their work and never stopping for a
chat—or piano recital—with humans. However, if The Piano teaches us anything, it is that
communication ranks up there with food, water, and shelter as an essential need. (It also teaches us
that Harvey Keitel has a lot of body confidence, but that's a matter for another book.)
Java programs don't have access to a piano. They use strings as the primary means to communicate
with users. Strings are collections of text—letters, numbers, punctuation, and other characters. During
this hour, you will learn all about working with strings in your Java programs. The following topics
will be covered:
\\ Backslash
\t Tab
\b Backspace
\r Carriage return
\f Formfeed
\n Newline
The newline character causes the text following the newline character to be displayed at the
beginning of the next line. Look at this example:
System.out.println("Music by\nMichael Nyman");
This statement would be displayed as the following:
Music by
Michael Nyman
In The Piano, Ada McGrath Stewart was thrown into unfamiliar territory when she moved from
Scotland to New Zealand to marry a stranger who didn't appreciate her ivory tickling. You might have
felt similarly lost with some of the topics introduced during this hour.
As a workshop to reinforce the string-handling features that have been covered, you will write a Java
program to display credits for a feature film. You have three guesses as to the movie chosen, and if
you need a hint, it starts with a The and ends with a musical instrument that can be used to express the
repressed passion of attractive mutes.
Load the word processor you're using to write Java programs and create a new file called Credits.java.
Enter the text of Listing 6.1 into the word processor and save the file when you're done.
1: class Credits {
2: public static void main(String[] arguments) {
3: // set up film information
4: String title = "The Piano";
5: int year = 1993;
6: String director = "Jane Campion";
7: String role1 = "Ada";
8: String actor1 = "Holly Hunter";
9: String role2 = "Baines";
10: String actor2 = "Harvey Keitel";
11: String role3 = "Stewart";
12: String actor3 = "Sam Neill";
13: String role4 = "Flora";
14: String actor4 = "Anna Paquin";
15: // display information
16: System.out.println(title + " (" + year + ")\n" +
17: "A " + director + " film.\n\n" +
18: role1 + "\t" + actor1 + "\n" +
19: role2 + "\t" + actor2 + "\n" +
20: role3 + "\t" + actor3 + "\n" +
21: role4 + "\t" + actor4);
22: }
23: }
Before you attempt to compile the program, look over the program and see whether you can figure out
what it's doing at each stage. Here's a breakdown of what's taking place:
Line 2 begins the main() block statement in which all of the program's work gets done.
Line 3 is a comment statement explaining that you're going to set up the film's information in
subsequent lines.
Lines 4–14 set up variables to hold information about the film, its director, and its stars. One
of the variables, year, is an integer. The rest are string variables.
Line 15 is another comment line for the benefit of humans like us who are examining the
program.
Lines 16–21 are one long System.out.println() statement. Everything between the first
parenthesis on Line 16 and the last parenthesis on Line 21 is displayed onscreen. The newline text
(\n) causes the text after it to be displayed at the beginning of a new line. The tab text (\t) inserts
tab spacing in the output. The rest are either text or string variables that should be shown.
If you're using the SDK, you can attempt to compile the program by going to the folder that contains
Credits.java and typing this command:
javac Credits.java
If you do not see any error messages, the program has compiled successfully, and you can run it with
the following command:
java Credits
If you do encounter error messages, correct any typos you find in your version of the Credits program
and try again to compile it.
Listing 6.2 shows the output of the Credits application: a rundown of the film, year of release,
director, and the four lead performers from The Piano. Be glad that you didn't have to present the
credits for an ensemble film. A program detailing Robert Altman's Short Cuts, the 1993 film with
more than 25 lead characters, could hog an hour of typing alone.
Once your version of Credits works like the one shown in Listing 6.2, give yourself some credit, too.
You're writing longer Java programs and dealing with more sophisticated issues each hour. Like
variables, strings are something you'll use every time you sit down to write a program.
At the beginning of The Piano, Ada lost her piano when her new husband refused to make his Maori
laborers carry it home. Luckily for you, the ability to use strings in your Java programs cannot be
taken away by an insensitive newlywed, or anyone else. You'll be using strings in many ways to
communicate with users.