4. ASCII Encoding For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70
5.
6. Character Processing Declaration and initialization char ch1, ch2 = ‘X’; Type conversion between int and char. System.out.print("ASCII code of character X is " + (int) ' X ' ); System.out.print ("Character with ASCII code 88 is " + (char)88 ); This comparison returns true because ASCII value of ' A ' is 65 while that of ' c ' is 99. ‘ A’ < ‘c’
7.
8.
9. Example: Counting Vowels char letter; String name = JOptionPane.showInputDialog(null, "Your name:" ); int numberOfCharacters = name.length () ; int vowelCount = 0; for ( int i = 0; i < numberOfCharacters; i++ ) { letter = name.charAt ( i ) ; if ( letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i' || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U' ) { vowelCount++; } } System.out.print ( name + ", your name has " + vowelCount + " vowels" ) ; Here’s the code to count the number of vowels in the input string.
10. Example: Counting ‘Java’ Continue reading words and count how many times the word Java occurs in the input, ignoring the case. int javaCount = 0; boolean repeat = true ; String word; while ( repeat ) { word = JOptionPane.showInputDialog ( null , "Next word:" ) ; if ( word.equals ( "STOP" ) ) { repeat = false; } else if ( word.equalsIgnoreCase ( "Java" ) ) { javaCount++; } } Notice how the comparison is done. We are not using the == operator.
11.
12.
13.
14. Regular Expression Examples Matches AZxx , CAxx , and COxx , where x is a single digit. (AZ|CA|CO)[0-9][0-9] Matches wad, weed, bad , and beed . [wb](ad|eed) A valid Java identifier consisting of alphanumeric characters, underscores, and dollar signs, with the first character being an alphabet. [a-zA-z][a-zA-Z0-9_$]* A single digit 0, 1, or 3. [013] A single character that is either a lowercase letter or a digit. [a-z0-9] A single digit that is 0, 1, 2, 3, 8, or 9. [0-9&&[^4567]] Any two-digit number from 00 to 99. [0-9][0-9] Description Expression
22. StringBuffer Example Changing a string Java to Diva StringBuffer word = new StringBuffer ( "Java" ) ; word.setCharAt ( 0, 'D' ) ; word.setCharAt ( 1, 'i' ) ; word : StringBuffer Java Before word : StringBuffer Diva After
23.
24.
25.
26.
27.
28. Design Document Another helper class for maintaining a word list. Details of this class can be found in Chapter 10. WordList Classes for pattern matching operations. Pattern/Matcher A helper class for opening a file and saving the result to a file. Details of this class can be found in Chapter 12. FileManager The key class of the program. An instance of this class managers other objects to build the word list. Ch9WordConcordance The instantiable main class of the program that implements the top-level program control. Ch9WordConcordanceMain Purpose Class
29. Class Relationships class we implement helper class given to us FileManger Ch9Word Concordance Matcher WordList Pattern Ch9Word ConcordanceMain (main class)
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
Notas del editor
We will cover the basic string processing in this lesson, manipulating char and String data.
Characters can be stored in a computer memory using the ASCII encoding. The ASCII codes range from 0 to 127. The character 'A' is represented as 65, for example. The ASCII values from 0 to 32 are called nonprintable control characters. For example, ASCII code 04 eot stands for End of Transmission. We can use this character to signal the end of transmission of data when sending data over a communication line.
ASCII works well for English-language documents because all characters and punctuation marks are included in the ASCII codes. But ASCII codes cannot be used to represent character sets of other languages. To overcome this limitation, unicode encoding was proposed. Unicode uses two bytes to represent characters and adopts the same encoding for the first 127 values as the ASCII codes. Encoding value of a character can be accessed by converting it to an int as the sample code illustrates.
We introduced the String class in Chapter 2. We will study additional String methods.
This sample code counts the number of vowels in a given input. Using the toUpperCase method, that converts all alphabetic characters to uppercase, we can rewrite the code as char letter; String name = inputBox.getString(&quot;What is your name?&quot;); int numberOfCharacters = name.length(); int vowelCount = 0; String nameUpper = name.toUpperCase(); for (int i = 0; i < numberOfCharacters; i++) { letter = nameUpper.charAt(i); if ( letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U' ) { vowelCount++; } } System.out.print(name + &quot;, your name has &quot; + vowelCount + &quot; vowels&quot;);
This sample code counts the number of times the word 'Java' is entered. Notice that we wrote word.equals( “STOP”) not word == “STOP” We described the differences in Lesson 5-2. In this situation, we need to use 'equals' because we are testing whether two String objects have the same sequence of characters.
We can use a pattern to represent a range of valid codes succintly. Without using the sample 3-digit pattern for computer science majors living on-campus, we must spell out 21 separate codes.
Regular expression allows us to express a large set of “words” (any sequence of symbols) succinctly. We use specially designated symbols such as the asterisk to formulate regular expressions.
Here are some examples of regular expressions.
The start method returns the position in the string where the first character of the pattern is found. The end method returns the value 1 more than the position in the string where the last character of the pattern is found.
By making String objects immutable, we can treat it much like a primitive data type for efficient processing. If we use the new operator to create a String object, a separate object is created as the top diagram illustrates. If we use a simple assignment as in the bottom diagram, then all literal constants refer to the same object. We can do this because String objects are immutable.
If the situation calls for directing changing the contents of a string, then we use the StringBuffer class.
This sample code illustrates how the original string is changed. Notice that the String class does not include the setCharAt method. The method is only defined in the mutable StringBuffer class.
Notice how the input routine is done. We are reading in a String object and converting it to a StringBuffer object, because we cannot simply assign a String object to a StringBuffer variable. For example, the following code is invalid: StringBuffer strBuffer = inputBox.getString( ); We are required to create a StringBuffer object from a String object as in String str = &quot;Hello&quot;; StringBuffer strBuf = new StringBuffer( str ); You cannot input StringBuffer objects. You have to input String objects and convert them to StringBuffer objects.
The append and insert are the two very useful methods of the StringBuffer class for string manipulation.
For this application, we are given two helper classes. The FileManager class handles the file input and output. The WordList class handles the maintenance of word lists. Our responsibility is to extract the words from a given document and use the helper classes correctly.
There will be a total of six key classes in this application. We will be designing two classes: Ch9WordConcordanceMain and Ch9WordConcordance.
Please use your Java IDE to view the source files and run the program.
For the second extension, the WordList class itself needs to be modified. Details on how to implement this extension can be found in Chapter 10 of the textbook.