SlideShare una empresa de Scribd logo
1 de 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]

Más contenido relacionado

La actualidad más candente

Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi_Kant_Sahu
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
myrajendra
 

La actualidad más candente (20)

Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java Strings
Java StringsJava Strings
Java Strings
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 

Similar a 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
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
wayn
 

Similar a 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
 
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
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de 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
 
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
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 

Más de 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
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
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
 

Último (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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?
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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
 
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...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

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.

Notas del editor

  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.