SlideShare a Scribd company logo
1 of 40
Chapter 9 Characters and Strings
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Characters ,[object Object],[object Object],[object Object],[object Object],[object Object]
ASCII Encoding For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70
Unicode Encoding ,[object Object],[object Object],char  ch1 = 'X'; System.out.println(ch1); System.out.println( (int) ch1); X 88
Character Processing Declaration and initialization char ch1, ch2 = ‘X’; Type conversion between int and char. System.out.print(&quot;ASCII code of character X is &quot; +  (int)  ' X '  ); System.out.print (&quot;Character with ASCII code 88 is &quot;  + (char)88 ); This comparison returns true because ASCII value of 'A' is 65 while that of 'c' is 99. ‘ A’ < ‘c’
Strings ,[object Object],[object Object],[object Object]
Accessing Individual Elements ,[object Object],0 1 2 3 4 5 6 S u m a t r a String name =  &quot;Sumatra&quot; ; name This variable refers to the whole string. name.charAt( 3 ) The method returns the character at position # 3.
Example: Counting Vowels char   letter; String  name  = JOptionPane.showInputDialog(null, &quot;Your name:&quot; ); 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 +  &quot;, your name has &quot;  + vowelCount +  &quot; vowels&quot; ) ; Here’s the code to count the number of vowels in the input string.
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 , &quot;Next word:&quot; ) ; if   (  word.equals ( &quot;STOP&quot; ) )   { repeat = false; }   else if   (  word.equalsIgnoreCase ( &quot;Java&quot; ) ) { javaCount++; } } Notice how the comparison is done. We are not using the  ==  operator.
Other Useful String Operators ,[object Object],Returns true if a string ends with a specified suffix string. str1.endsWith( str2 ) Returns true if a string starts with a specified prefix string. str1.startsWith( str2 ) Converts a given primitive data value to a string. String.valueOf( 123.4565 ) Removes the leading and trailing spaces. str1.trim( ) Extracts the a substring from a string. str1.substring( 1, 4 ) Compares the two strings. str1.compareTo( str2 ) Meaning endsWith startsWith valueOf trim substring compareTo Method
Pattern Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The 3-digit pattern to represent computer science majors living on-campus is 5[123][1-7] first character is 5 second character is 1, 2, or 3 third character is any digit  between 1 and 7
Regular Expressions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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
The replaceAll Method ,[object Object],Replace all vowels with the symbol @ String originalText, modifiedText; originalText = ...;  //assign string modifiedText =  originalText.replaceAll ( &quot;[aeiou]&quot; , &quot;@&quot; ) ;
The  Pattern  and  Matcher  Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The  compile  Method ,[object Object],[object Object],[object Object]
The  find  Method ,[object Object],[object Object],[object Object],[object Object]
The String Class is Immutable ,[object Object],[object Object],[object Object],[object Object]
Effect of Immutability We can do this because String objects are immutable.
The  StringBuffer  Class ,[object Object],[object Object]
StringBuffer Example Changing a string  Java  to  Diva StringBuffer word =  new  StringBuffer ( &quot;Java&quot; ) ; word.setCharAt ( 0, 'D' ) ; word.setCharAt ( 1, 'i' ) ; word : StringBuffer Java Before word : StringBuffer Diva After
Sample Processing ,[object Object],char    letter; String  inSentence  = JOptionPane.showInputDialog ( null ,  &quot;Sentence:&quot; ) ; StringBuffer tempStringBuffer  =  new  StringBuffer ( inSentence ) ; int   numberOfCharacters = tempStringBuffer.length () ; for   ( int  index = 0; index < numberOfCharacters; index++ ) { letter = tempStringBuffer.charAt ( index ) ; if   ( letter ==  'a'  || letter ==  'A'  || letter ==  'e'  || letter ==  'E'  || letter ==  'i'  || letter ==  'I'  || letter ==  'o'  || letter ==  'O'  || letter ==  'u'  || letter ==  'U'   ) { tempStringBuffer.setCharAt ( index, 'X' ) ; } } JOptionPane.showMessageDialog ( null , tempStringBuffer  ) ;
The  append  and  insert  Methods ,[object Object],[object Object],[object Object],[object Object]
The StringBuilder Class ,[object Object],[object Object],[object Object],[object Object],[object Object]
Problem Statement ,[object Object],[object Object]
Overall Plan ,[object Object],while   (   the user wants to process  another file   ) { T ask 1: read the file ; Task 2: build the word list ; Task 3: save the word list to a file ; }
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
Class Relationships class we implement helper class given to us FileManger Ch9Word Concordance Matcher WordList Pattern Ch9Word ConcordanceMain (main class)
Development Steps ,[object Object],[object Object],[object Object],[object Object],[object Object]
Step 1 Design ,[object Object],[object Object]
Step 1 Code ,[object Object],[object Object],[object Object],Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.
Step 1 Test ,[object Object]
Step 2 Design ,[object Object],[object Object],[object Object]
Step 2 Code ,[object Object],[object Object],[object Object]
Step 2 Test ,[object Object],[object Object],[object Object]
Step 3 Design ,[object Object],[object Object],[object Object]
Step 3 Code ,[object Object],[object Object],[object Object]
Step 3 Test ,[object Object],[object Object],[object Object]
Step 4: Finalize ,[object Object],[object Object],[object Object]

More Related Content

What's hot

Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
Navtar Sidhu Brar
 

What's hot (20)

Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Java.util
Java.utilJava.util
Java.util
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Hamiltonian path
Hamiltonian pathHamiltonian path
Hamiltonian path
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Sorting in python
Sorting in python Sorting in python
Sorting in python
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Hashing
HashingHashing
Hashing
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Stack
StackStack
Stack
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 

Similar to Chapter 9 - Characters and Strings

Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips  Some hints for approa.docxCSE 220 Assignment 3 Hints and Tips  Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
faithxdunce63732
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 

Similar to Chapter 9 - Characters and Strings (20)

M C6java7
M C6java7M C6java7
M C6java7
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
String handling
String handlingString handling
String handling
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Javascript
JavascriptJavascript
Javascript
 
07slide
07slide07slide
07slide
 
Javascript
JavascriptJavascript
Javascript
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
String notes
String notesString notes
String notes
 
Ch09
Ch09Ch09
Ch09
 
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips  Some hints for approa.docxCSE 220 Assignment 3 Hints and Tips  Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Array &strings
Array &stringsArray &strings
Array &strings
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
 
Php, mysqlpart2
Php, mysqlpart2Php, mysqlpart2
Php, mysqlpart2
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 

More from Eduardo Bergavera (10)

CLP Session 5 - The Christian Family
CLP Session 5 - The Christian FamilyCLP Session 5 - The Christian Family
CLP Session 5 - The Christian Family
 
What is Python?
What is Python?What is Python?
What is Python?
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software Development
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Chapter 9 - Characters and Strings

  • 1. Chapter 9 Characters and Strings
  • 2.
  • 3.
  • 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(&quot;ASCII code of character X is &quot; + (int) ' X ' ); System.out.print (&quot;Character with ASCII code 88 is &quot; + (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, &quot;Your name:&quot; ); 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 + &quot;, your name has &quot; + vowelCount + &quot; vowels&quot; ) ; 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 , &quot;Next word:&quot; ) ; if ( word.equals ( &quot;STOP&quot; ) ) { repeat = false; } else if ( word.equalsIgnoreCase ( &quot;Java&quot; ) ) { 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
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20. Effect of Immutability We can do this because String objects are immutable.
  • 21.
  • 22. StringBuffer Example Changing a string Java to Diva StringBuffer word = new StringBuffer ( &quot;Java&quot; ) ; 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.

Editor's Notes

  1. We will cover the basic string processing in this lesson, manipulating char and String data.
  2. Characters can be stored in a computer memory using the ASCII encoding. The ASCII codes range from 0 to 127. The character &apos;A&apos; 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.
  3. 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.
  4. We introduced the String class in Chapter 2. We will study additional String methods.
  5. 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(&amp;quot;What is your name?&amp;quot;); int numberOfCharacters = name.length(); int vowelCount = 0; String nameUpper = name.toUpperCase(); for (int i = 0; i &lt; numberOfCharacters; i++) { letter = nameUpper.charAt(i); if ( letter == &apos;A&apos; || letter == &apos;E&apos; || letter == &apos;I&apos; || letter == &apos;O&apos; || letter == &apos;U&apos; ) { vowelCount++; } } System.out.print(name + &amp;quot;, your name has &amp;quot; + vowelCount + &amp;quot; vowels&amp;quot;);
  6. This sample code counts the number of times the word &apos;Java&apos; 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 &apos;equals&apos; because we are testing whether two String objects have the same sequence of characters.
  7. Here are some examples: String str1 = “Java”, str2 = “ Wow “; str1.compareTo( “Hello” ); //returns positive integer //because str1 &gt;= “Hello” str1.substring( 1, 4 ); //returns “ava” str2.trim( ) //returns “Wow”, str2 stays same str1.startsWith( “Ja” ); //returns true str1.endsWith( “avi” ); //returns false
  8. 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.
  9. 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.
  10. Here are some examples of regular expressions.
  11. 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.
  12. 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.
  13. If the situation calls for directing changing the contents of a string, then we use the StringBuffer class.
  14. 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.
  15. 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 = &amp;quot;Hello&amp;quot;; StringBuffer strBuf = new StringBuffer( str ); You cannot input StringBuffer objects. You have to input String objects and convert them to StringBuffer objects.
  16. The append and insert are the two very useful methods of the StringBuffer class for string manipulation.
  17. 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.
  18. There will be a total of six key classes in this application. We will be designing two classes: Ch9WordConcordanceMain and Ch9WordConcordance.
  19. Please use your Java IDE to view the source files and run the program.
  20. 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.