Counting Characters in Strings

The letters that appear most often in English are E, R, S, T, L, N, C, D, M, and O, in that order. This is a fact worth knowing if you ever find yourself on the syndicated game show Wheel of Fortune.


Note

If you’re unfamiliar with the show, Wheel of Fortune is a game in which three contestants guess the letters of a phrase, name, or quote. If they get a letter right and it’s a consonant, they win the amount of money spun on a big wheel. To re-create the experience, play hangman with your friends in front of a studio audience, hand out random amounts of money when someone guesses a letter correctly, and give the winner a new Amana stove.


The next program you create this hour counts letter frequency in as many different phrases and expressions as you care to type. An array is used to count the number of times that each letter appears. When you’re done, the program presents the number of times each letter appeared in the phrases.

Create a new Empty Java File in NetBeans called Wheel.java, fill it with the contents of Listing 9.3 and save the file when you’re finished. Feel free to add additional phrases between Lines 17 and 18, formatting them exactly like Line 17.

Listing 9.3. The Full Source Code of Wheel.java


 1: class Wheel {
 2:     public static void main(String[] args) {
 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:             "I LIKE IKE",
10:             "PLAY IT AGAIN, SAM",
11:             "FROSTY THE SNOWMAN",
12:             "ONE MORE FOR THE ROAD",
13:             "HOME FIELD ADVANTAGE",
14:             "VALENTINE'S DAY MASSACRE",
15:             "GROVER CLEVELAND OHIO",
16:             "SPAGHETTI WESTERN",
17:             "AQUA TEEN HUNGER FORCE",
18:             "IT'S A WONDERFUL LIFE"
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:                 " ");
35:         }
36:         System.out.println();
37:     }
38: }


If you run the program without adding your own phrases, the output should resemble Listing 9.4.

Listing 9.4. 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 3–19: Phrases are stored in a string array called phrase.

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

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

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

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

• Line 24: A for loop 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 is used to determine its element number.

• Lines 26–28: An if statement weeds 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 31: A for loop cycles 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.

This 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. 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. ASCII is limited to just 256.


Summary

Arrays make it possible to store complicated types of information in a program and manipulate that information. They’re ideal for anything that can be arranged in a list and can be accessed easily using the loop statements that you learned about during Hour 8, “Repeating an Action with Loops.”

To be honest, the information processing needs of Santa Claus probably have outgrown arrays. More children are manufactured each year, and the gifts they want are increasing in complexity and expense.

Your programs are likely to use arrays to store information that is unwieldy to work with using variables, even if you’re not making any lists or checking them twice.

Q&A

Q. Is the numeric range of the alphabet, from 65 for A to 90 for Z, part of the basic Java language? If so, what are 1 through 64 reserved for?

A. The numbers 1 through 64 include numerals, punctuation marks, and some unprintable characters, such as linefeed, newline, and backspace. A number is associated with each printable character that can be used in a Java program, as well as some unprintable ones. Java uses the Unicode numbering system. The first 127 characters are from the ASCII character set, which you might have used in another programming language.

Q. Why are some errors called exceptions?

A. The significance of the term is that a program normally runs without any problems, and the exception signals an exceptional circumstance that must be dealt with. Exceptions are warning  messages that are sent from within a Java program. In the Java language, the term error is sometimes confined to describe error conditions that take place within the interpreter running a program. You learn more about both subjects during Hour 18.

Q. In a multidimensional array, is it possible to use the length variable to measure different dimensions other than the first?

A. You can test any dimension of the array. For the first dimension, use length with the name of the array, as in x.length. Subsequent dimensions can be measured by using length with the [0] element of that dimension. Consider an array called data that was created with the following statement:

int[][][] data = new int[12][13][14];

The dimensions of this array can be measured by using the data.length variable for the first dimension, data[0].length for the second, and data[0][0].length for the third.

Q. Why does New England Patriots head coach Bill Belichick always wear that ridiculous hoodie on the sidelines?

A. Sportswriters believe that Belichick began wearing the attire in response to an NFL deal with Reebok that required all coaches to wear licensed team apparel.

“He decided that if they were going to make him wear team apparel then he’d sift through the options and put on the absolute ugliest thing he could find,” Dan Wetzel of Yahoo! Sports explained in 2007. “He chose a grey sweatshirt, often with a hood.”

Belichick’s passive-aggressive fashion statement has turned the hoodie into one of the team’s best-selling items.

Workshop

If the brain were an array, you could test its length by answering each of the following questions about arrays.

Quiz

1. What types of information are arrays best suited for?

A. Lists

B. Pairs of related information

C. Trivia

2. What variable can you use to check the upper boundary of an array?

A. top

B. length

C. limit

3. How many reindeer does Santa have, including Rudolph?

A. 8

B. 9

C. 10

Answers

1. A. Lists that contain nothing but the same type of information—strings, numbers, and so on—are well-suited for storage in arrays.

2. B. The length variable contains a count of the number of elements in an array.

3. B. Santa had “eight tiny reindeer,” according to Clement Clarke Moore’s “A Visit from St. Nicholas,” so Rudolph makes nine.

Activities

To give yourself an array of experiences to draw from later, you can expand your knowledge of this hour’s topics with the following activities:

• Create a program that uses a multidimensional array to store student grades. The first dimension should be a number for each student, and the second dimension should be for each student’s grades. Display the average of all the grades earned by each student and an overall average for every student.

• Write a program that stores the first 400 numbers that are multiples of 13 in an array.

To see Java programs that implement these activities, visit the book’s website at www.java24hours.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.214.27