# Storing Information with Java Arrays

• Print
This chapter is from the book

## Workshop: Array of Prizes, Indeed

Watch the syndicated game show Wheel of Fortune for any length of time and you'll be surprised at how predictable the contestants are.

In the years that this show has been one of the world's most successful television programs, fortune seekers have worked the puzzle-solving process into an exact science.

Wheel contestants typically guess the same letters when they are starting out on a puzzle: R, S, T, L, and N. In the final round, when these letters and the vowel E are given to the players right away, they usually choose four other letters: C, D, M, and the vowel O.

The reason for this predictability is that these are the letters that appear most often in English words. The contestants are stifling their desire for spontaneity in order to better their chances to win a trip to Bermuda, cash, and a Yamaha Waverunner.

NOTE

In case you're unfamiliar with the show, Wheel of Fortune is a game in which three contestants try to guess the letters of a phrase, name, quote, or other memorable item. If they get a letter right and it's a consonant, they win the amount of money they spun on a big wheel. To re-create the experience, play hangman with some of your friends in front of a studio audience, hand out random amounts of money when someone guesses a letter in the secret word or phrase, and give the winner a new Chevy Suburban.

Your Java workshop during this hour will test the most-common-letter theory by looking at as many different phrases and expressions as you care to type. An array will be used to count the number of times that each letter appears. When you're done, the program will present each letter and the number of times it appears in the phrases you entered. It also will present some clues about which letters to avoid entirely (unless you suspect that a puzzle's answer is the Aztec priest-ruler Quetzalcoatl or the fire god Xiuhtecuhtle).

Open up a new file in your word processor and call it Wheel.java. Enter Listing 9.2 and save the file when you're done.

#### Listing 9.2 The Full Source Code of Wheel.java

``` 1: class Wheel {
2:  public static void main(String[] arguments) {
3:    String phrase[] = {
4:      "A STITCH IN TIME SAVES NINE",
5:      "DON'T EAT YELLOW SNOW",
6:      "JUST DO IT",
7:      "EVERY GOOD BOY DOES FINE",
8:      "I WANT MY MTV",
9:      "HOW 'BOUT THEM COWBOYS",
10:      "PLAY IT AGAIN, SAM",
11:      "FROSTY THE SNOWMAN",
12:      "ONE MORE FOR THE ROAD",
14:      "VALENTINE'S DAY MASSACRE",
15:      "GROVER CLEVELAND OHIO",
16:      "WONDERFUL WORLD OF DISNEY",
17:      "COAL MINER'S DAUGHTER",
18:      "WILL IT PLAY IN PEORIA"
19:    };
20:    int[] letterCount = new int[26];
21:    for (int count = 0; count < phrase.length; count++) {
22:      String current = phrase[count];
23:      char[] letters = current.toCharArray();
24:      for (int count2 = 0; count2 < letters.length; count2++) {
25:        char lett = letters[count2];
26:        if ( (lett >= 'A') & (lett <= 'Z') ) {
27:          letterCount[lett - 'A']++;
28:        }
29:      }
30:     }
31:    for (char count = 'A'; count <= 'Z'; count++) {
32:      System.out.print(count + ": " +
33:          letterCount[count - 'A'] +
34:          "\t");
35:    }
36:    System.out.println();
37:  }
38: }```

After you compile the file and run it, the output should resemble Listing 9.3.

#### Listing 9.3 Output of the Wheel Program

```A: 22  B: 3  C: 5  D: 13  E: 28  F: 6  G: 5  H: 8  I: 18
J: 1  K: 0  L: 13  M: 10  N: 19  O: 27  P: 3  Q: 0  R: 13
S: 15  T: 19  U: 4  V: 7  W: 9  X: 0  Y: 10  Z: 0```

The following things are taking place in the Wheel program:

• Lines 1 and 2: The Wheel program and the main() block of the program begin.

• Lines 3–19: Phrases are stored in a string array called phrase. Every phrase between the { on Line 3 and the } on Line 19 will be stored in its own element of the array, beginning with A STITCH IN TIME SAVES NINE in phrase[0].

• Line 20: An integer array called letterCount is created with 26 elements. This array will be used to store the number of times each letter appears. The order of the elements is from A to Z. letterCount[0] will store the count for letter A, letterCount[1] will store the count for B, and so on, up to letterCount[25] for Z.

• Line 21: A for loop is begun that cycles through the phrases stored in the phrase array. The phrase.length variable is used in the for statement to end the loop after the last phrase is reached.

• Line 22: A string variable named current is created and set with the value of the current element of the phrase array.

• Line 23: A character array is created that stores all of the characters in the current phrase.

• Line 24: A for loop is begun that cycles through the letters of the current phrase. The letters.length variable is used to end the loop after the last letter is reached.

• Line 25: A character variable called lett is created with the value of the current letter. In addition to their text value, characters have a numeric value. Because elements of an array are numbered, the numeric value of each character will be used to determine its element number.

• Lines 26–28: An if statement is used to weed out all characters that are not part of the alphabet, such as punctuation and spaces. An element of the letterCount array is increased by 1 depending on the numeric value of the current character, which is stored in lett. The numeric values of the alphabet range from 65 for 'A' to 90 for 'Z'. Because the letterCount array begins at 0 and ends at 25, 'A' (65) is subtracted from lett to determine which array element to increase.

• Line 29: One pass through the inner for loop ends, and the loop returns to Line 24 to get the next letter in the current phrase, if there is one.

• Line 30: One pass through the outer for loop ends, and the loop returns to Line 21 to get the next phrase, if there is one.

• Line 31: A for loop is used to cycle through the alphabet from 'A' to 'Z'.

• Lines 32–34: The current letter is displayed followed by a semicolon and the number of times the letter appeared in the phrases stored in the phrase array. The \t inserts a tab character.

• Line 35: One pass through the for loop ends, and the loop returns to Line 31 to get the next character in the alphabet, unless 'Z' has been reached.

• Lines 36–38: A blank line is displayed, followed by the end of the program's main() block, and the end of the program.

This workshop project shows how two nested for loops can be used to cycle through a group of phrases one letter at a time. Java attaches a numeric value to each character; this value is easier to use than the character inside arrays.

NOTE

The numeric values associated with each of the characters from A to Z are those used by the ASCII character set. ASCII is a standard method of arranging and numbering letters, numbers, punctuation, and other symbols that can be represented by a computer. The ASCII character set is part of Unicode, the full character set supported by the Java language. Unicode includes support for more than 60,000 different characters used in the world's written languages.

Using the length variable makes it possible for you to add as many phrases as desired within the { and } marks. The letters in each of the new phrases you add will be analyzed, and you can build up a better idea of what it takes to make a small fortune in 30 minutes on television.