SlideShare a Scribd company logo
Chapter 9 Characters and Strings
Objectives After you have read and studied this chapter, you should be able to  Declare and manipulate data of the char data type. Write string processing program using String, StringBuilder, and StringBuffer objects. Differentiate the three string classes and use the correct class for a given task. Specify regular expressions for searching a pattern in a string. Use the Pattern and Matcher classes. Compare the String objects correctly.
Characters In Java, single characters are represented using the data type  char . Character constants are written as symbols enclosed in single quotes. Characters are stored in a computer memory using some form of encoding. ASCII , which stands for  American Standard Code for Information Interchange , is one of the document coding schemes widely used today. Java uses  Unicode , which includes ASCII, for representing  char  constants.
ASCII Encoding For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70
Unicode Encoding The  Unicode Worldwide Character Standard  ( Unicode ) supports the interchange, processing, and display of the written texts of diverse languages. Java uses the Unicode standard for representing  char  constants. 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 A  string  is a sequence of characters that is treated as a single value. Instances of the  String  class are used to represent strings in Java. We can access individual characters of a string by calling the  charAt  method of the  String  object.
Accessing Individual Elements Individual characters in a String accessed with the  charAt  method. 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 See the  String  class documentation for details. 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 Suppose students are assigned a three-digit code: The first digit represents the major (5 indicates computer science); The second digit represents either in-state (1), out-of-state (2), or foreign (3); The third digit indicates campus housing:  On-campus dorms are numbered 1-7. Students living off-campus are represented by the digit 8. 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 The pattern is called a  regular expression . Rules The brackets  [ ]  represent choices The asterisk symbol  *  means zero or more occurrences.  The plus symbol  +  means one or more occurrences. The hat symbol  ^  means negation. The hyphen  –  means ranges.  The parentheses  ( )  and the vertical bar  |  mean a range of choices for multiple characters.
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 The  replaceAll  method replaces all occurrences of a substring that matches a given regular expression with a given replacement string. 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 The  matches  and  replaceAll  methods of the String class are shorthand for using the  Pattern  and  Matcher  classes from the  java.util.regex  package. If str and regex are String objects, then str.matches ( regex ) ; is equivalent to  Pattern pattern = Pattern.compile ( regex ) ; Matcher matcher = pattern.matcher ( str ) ; matcher.matches () ;
The  compile  Method The  compile  method of the Pattern class converts the stated regular expression to an internal format to carry out the pattern-matching operation.  This conversion is carried out every time the  matches  method of the String class is executed, so it is more efficient to use the compile method when we search for the same pattern multiple times. See the sample programs  Ch9MatchJavaIdentifier2  and  Ch9PMCountJava
The  find  Method The  find  method is another powerful method of the  Matcher  class. It searches for the next sequence in a string that matches the pattern, and returns true if the pattern is found. When a matcher finds a matching sequence of characters, we can query the location of the sequence by using the  start  and  end  methods. See  Ch9PMCountJava2
The String Class is Immutable In Java a String object is immutable This means once a String object is created, it cannot be changed, such as replacing a character with another character or removing a character The String methods we have used so far do not change the original string. They created a new string from the original. For example, substring creates a new string from a given string. The String class is defined in this manner for efficiency reason.
Effect of Immutability We can do this because String objects are immutable.
The  StringBuffer  Class In many string processing applications, we would like to change the contents of a string. In other words, we want it to be mutable. Manipulating the content of a string, such as replacing a character, appending a string with another string, deleting a portion of a string, and so on, may be accomplished by using the  StringBuffer  class.
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 Replace all vowels in the sentence with ‘X’.  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 We use the  append  method to append a String or StringBuffer object to the end of a StringBuffer object.  The method can also take an argument of the primitive data type. Any primitive data type argument is converted to a string before it is appended to a StringBuffer object. We can insert a string at a specified position by using the  insert  method.
The StringBuilder Class This class is new to Java 5.0 (SDK 1.5) The class is added to the newest version of Java to improve the performance of the StringBuffer class. StringBuffer and StringBuilder support exactly the same set of methods, so they are interchangeable. There are advanced cases where we must use StringBuffer, but all sample applications in the book, StringBuilder can be used. Since the performance is not our main concern and that the StringBuffer class is usable for all versions of Java, we will use StringBuffer only in this book.
Problem Statement Problem statement: Write an application that will build a word concordance of a document. The output from the application is an alphabetical list of all words in the given document and the number of times they occur in the document. The documents are a text file (contents of the file are an ASCII characters) and the output of the program is saved as an ASCII file also.
Overall Plan Tasks expressed in pseudocode: 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 We will develop this program in four steps: Start with a program skeleton. Define the main class with data members. Begin with a rudimentary Ch9WordConcordance class. Add code to open a file and save the result. Extend the existing classes as necessary. Complete the implemention of the Ch9WordConcordance class. Finalize the code by removing temporary statements and tying up loose ends.
Step 1 Design Define the skeleton main class Define the skeleton Ch9WordConcordance class that has only an empty zero-argument constructor
Step 1 Code Directory:   Chapter9/Step1 Source Files:  Ch9WordConcordanceMain.java   Ch9WordConcordance.java 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 The purpose of Step 1 testing is to verify that the constructor is executed correctly and the repetition control in the  start  method works as expected.
Step 2 Design Design and implement the code to open and save a file The actual tasks are done by the  FileManager  class, so our objective in this step is to find out the correct usage of the  FileManager  helper class. The  FileManager  class has two key methods:  openFile  and  saveFile .
Step 2 Code Directory:   Chapter9/Step2 Source Files:  Ch9WordConcordanceMain.java   Ch9WordConcordance.java
Step 2 Test The Step2 directory contains several sample input files. We will open them and verify the file contents are read correctly by checking the temporary echo print output to System.out.  To verify the output routine, we save to the output (the temporary output created by the build method of Ch9WordConcordance) and verify its content.  Since the output is a textfile, we can use any word processor or text editor to view its contents.
Step 3 Design Complete the  build  method of Ch9WordConcordance class. We will use the second helper class  WordList  here, so we need to find out the details of this helper class. The key method of the  WordList  class is the  add  method that inserts a given word into a word list.
Step 3 Code Directory:   Chapter9/Step3 Source Files:  Ch9WordConcordanceMain.java   Ch9WordConcordance.java
Step 3 Test We run the program against varying types of input textfiles.  We can use a long document such as the term paper for the last term’s economy class (don’t forget to save it as a textfile before testing).  We should also use some specially created files for testing purposes. One file may contain one word repeated 7 times, for example. Another file may contain no words at all.
Step 4: Finalize Possible Extensions One is an integrated user interface where the end user can view both the input document files and the output word list files.  Another is the generation of different types of list. In the sample development, we count the number of occurences of each word. Instead, we can generate a list of positions where each word appears in the document.
Ad

More Related Content

What's hot (20)

Class diagrams
Class diagramsClass diagrams
Class diagrams
Nadia_Nazeer
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
NainaKhan28
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
Vikram Nandini
 
Python-DataAbstarction.pptx
Python-DataAbstarction.pptxPython-DataAbstarction.pptx
Python-DataAbstarction.pptx
Karudaiyar Ganapathy
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
Tirthika Bandi
 
Java Constructors
Java ConstructorsJava Constructors
Java Constructors
Saumya Som
 
Networking concepts by Sachidananda M H
Networking concepts by Sachidananda M HNetworking concepts by Sachidananda M H
Networking concepts by Sachidananda M H
Sachidananda M H
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
CSCI-383 Lecture 3-4: Abstraction
CSCI-383 Lecture 3-4: AbstractionCSCI-383 Lecture 3-4: Abstraction
CSCI-383 Lecture 3-4: Abstraction
JI Ruan
 
DBMS 9 | Extendible Hashing
DBMS 9 | Extendible HashingDBMS 9 | Extendible Hashing
DBMS 9 | Extendible Hashing
Mohammad Imam Hossain
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
Azeemaj101
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
Michael Heron
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
types conversion in python with exa.pptx
types conversion in python with exa.pptxtypes conversion in python with exa.pptx
types conversion in python with exa.pptx
urvashipundir04
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
NainaKhan28
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
Tirthika Bandi
 
Java Constructors
Java ConstructorsJava Constructors
Java Constructors
Saumya Som
 
Networking concepts by Sachidananda M H
Networking concepts by Sachidananda M HNetworking concepts by Sachidananda M H
Networking concepts by Sachidananda M H
Sachidananda M H
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
CSCI-383 Lecture 3-4: Abstraction
CSCI-383 Lecture 3-4: AbstractionCSCI-383 Lecture 3-4: Abstraction
CSCI-383 Lecture 3-4: Abstraction
JI Ruan
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
Azeemaj101
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
Michael Heron
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
types conversion in python with exa.pptx
types conversion in python with exa.pptxtypes conversion in python with exa.pptx
types conversion in python with exa.pptx
urvashipundir04
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 

Similar to Chapter 9 - Characters and Strings (20)

M C6java7
M C6java7M C6java7
M C6java7
mbruggen
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 
String handling
String handlingString handling
String handling
ssuser20c32b
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
BG Java EE Course
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Bernard Loire
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
07slide
07slide07slide
07slide
Aboudi Sabbah
 
Javascript
JavascriptJavascript
Javascript
vikram singh
 
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
İbrahim Kürce
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
Ch09
Ch09Ch09
Ch09
Arriz San Juan
 
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
 
Array &strings
Array &stringsArray &strings
Array &strings
UMA PARAMESWARI
 
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...
Indu32
 
Php, mysqlpart2
Php, mysqlpart2Php, mysqlpart2
Php, mysqlpart2
Subhasis Nayak
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
SachinBhosale73
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Bernard Loire
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 
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
İbrahim Kürce
 
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
 
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...
Indu32
 
Ad

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
Eduardo Bergavera
 
What is Python?
What is Python?What is Python?
What is Python?
Eduardo Bergavera
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
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
Eduardo Bergavera
 
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
Eduardo Bergavera
 
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
Eduardo Bergavera
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
Eduardo Bergavera
 
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
Eduardo Bergavera
 
CLP Session 5 - The Christian Family
CLP Session 5 - The Christian FamilyCLP Session 5 - The Christian Family
CLP Session 5 - The Christian Family
Eduardo Bergavera
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
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
Eduardo Bergavera
 
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
Eduardo Bergavera
 
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
Eduardo Bergavera
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
Eduardo Bergavera
 
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
Eduardo Bergavera
 
Ad

Recently uploaded (20)

On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
CSUC - Consorci de Serveis Universitaris de Catalunya
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 

Chapter 9 - Characters and Strings

  • 1. Chapter 9 Characters and Strings
  • 2. Objectives After you have read and studied this chapter, you should be able to Declare and manipulate data of the char data type. Write string processing program using String, StringBuilder, and StringBuffer objects. Differentiate the three string classes and use the correct class for a given task. Specify regular expressions for searching a pattern in a string. Use the Pattern and Matcher classes. Compare the String objects correctly.
  • 3. Characters In Java, single characters are represented using the data type char . Character constants are written as symbols enclosed in single quotes. Characters are stored in a computer memory using some form of encoding. ASCII , which stands for American Standard Code for Information Interchange , is one of the document coding schemes widely used today. Java uses Unicode , which includes ASCII, for representing char constants.
  • 4. ASCII Encoding For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70
  • 5. Unicode Encoding The Unicode Worldwide Character Standard ( Unicode ) supports the interchange, processing, and display of the written texts of diverse languages. Java uses the Unicode standard for representing char constants. char ch1 = 'X'; System.out.println(ch1); System.out.println( (int) ch1); X 88
  • 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. Strings A string is a sequence of characters that is treated as a single value. Instances of the String class are used to represent strings in Java. We can access individual characters of a string by calling the charAt method of the String object.
  • 8. Accessing Individual Elements Individual characters in a String accessed with the charAt method. 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.
  • 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. Other Useful String Operators See the String class documentation for details. 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
  • 12. Pattern Example Suppose students are assigned a three-digit code: The first digit represents the major (5 indicates computer science); The second digit represents either in-state (1), out-of-state (2), or foreign (3); The third digit indicates campus housing: On-campus dorms are numbered 1-7. Students living off-campus are represented by the digit 8. 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
  • 13. Regular Expressions The pattern is called a regular expression . Rules The brackets [ ] represent choices The asterisk symbol * means zero or more occurrences. The plus symbol + means one or more occurrences. The hat symbol ^ means negation. The hyphen – means ranges. The parentheses ( ) and the vertical bar | mean a range of choices for multiple characters.
  • 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. The replaceAll Method The replaceAll method replaces all occurrences of a substring that matches a given regular expression with a given replacement string. Replace all vowels with the symbol @ String originalText, modifiedText; originalText = ...; //assign string modifiedText = originalText.replaceAll ( &quot;[aeiou]&quot; , &quot;@&quot; ) ;
  • 16. The Pattern and Matcher Classes The matches and replaceAll methods of the String class are shorthand for using the Pattern and Matcher classes from the java.util.regex package. If str and regex are String objects, then str.matches ( regex ) ; is equivalent to Pattern pattern = Pattern.compile ( regex ) ; Matcher matcher = pattern.matcher ( str ) ; matcher.matches () ;
  • 17. The compile Method The compile method of the Pattern class converts the stated regular expression to an internal format to carry out the pattern-matching operation. This conversion is carried out every time the matches method of the String class is executed, so it is more efficient to use the compile method when we search for the same pattern multiple times. See the sample programs Ch9MatchJavaIdentifier2 and Ch9PMCountJava
  • 18. The find Method The find method is another powerful method of the Matcher class. It searches for the next sequence in a string that matches the pattern, and returns true if the pattern is found. When a matcher finds a matching sequence of characters, we can query the location of the sequence by using the start and end methods. See Ch9PMCountJava2
  • 19. The String Class is Immutable In Java a String object is immutable This means once a String object is created, it cannot be changed, such as replacing a character with another character or removing a character The String methods we have used so far do not change the original string. They created a new string from the original. For example, substring creates a new string from a given string. The String class is defined in this manner for efficiency reason.
  • 20. Effect of Immutability We can do this because String objects are immutable.
  • 21. The StringBuffer Class In many string processing applications, we would like to change the contents of a string. In other words, we want it to be mutable. Manipulating the content of a string, such as replacing a character, appending a string with another string, deleting a portion of a string, and so on, may be accomplished by using the StringBuffer class.
  • 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. Sample Processing Replace all vowels in the sentence with ‘X’. 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 ) ;
  • 24. The append and insert Methods We use the append method to append a String or StringBuffer object to the end of a StringBuffer object. The method can also take an argument of the primitive data type. Any primitive data type argument is converted to a string before it is appended to a StringBuffer object. We can insert a string at a specified position by using the insert method.
  • 25. The StringBuilder Class This class is new to Java 5.0 (SDK 1.5) The class is added to the newest version of Java to improve the performance of the StringBuffer class. StringBuffer and StringBuilder support exactly the same set of methods, so they are interchangeable. There are advanced cases where we must use StringBuffer, but all sample applications in the book, StringBuilder can be used. Since the performance is not our main concern and that the StringBuffer class is usable for all versions of Java, we will use StringBuffer only in this book.
  • 26. Problem Statement Problem statement: Write an application that will build a word concordance of a document. The output from the application is an alphabetical list of all words in the given document and the number of times they occur in the document. The documents are a text file (contents of the file are an ASCII characters) and the output of the program is saved as an ASCII file also.
  • 27. Overall Plan Tasks expressed in pseudocode: 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 ; }
  • 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. Development Steps We will develop this program in four steps: Start with a program skeleton. Define the main class with data members. Begin with a rudimentary Ch9WordConcordance class. Add code to open a file and save the result. Extend the existing classes as necessary. Complete the implemention of the Ch9WordConcordance class. Finalize the code by removing temporary statements and tying up loose ends.
  • 31. Step 1 Design Define the skeleton main class Define the skeleton Ch9WordConcordance class that has only an empty zero-argument constructor
  • 32. Step 1 Code Directory: Chapter9/Step1 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.
  • 33. Step 1 Test The purpose of Step 1 testing is to verify that the constructor is executed correctly and the repetition control in the start method works as expected.
  • 34. Step 2 Design Design and implement the code to open and save a file The actual tasks are done by the FileManager class, so our objective in this step is to find out the correct usage of the FileManager helper class. The FileManager class has two key methods: openFile and saveFile .
  • 35. Step 2 Code Directory: Chapter9/Step2 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java
  • 36. Step 2 Test The Step2 directory contains several sample input files. We will open them and verify the file contents are read correctly by checking the temporary echo print output to System.out. To verify the output routine, we save to the output (the temporary output created by the build method of Ch9WordConcordance) and verify its content. Since the output is a textfile, we can use any word processor or text editor to view its contents.
  • 37. Step 3 Design Complete the build method of Ch9WordConcordance class. We will use the second helper class WordList here, so we need to find out the details of this helper class. The key method of the WordList class is the add method that inserts a given word into a word list.
  • 38. Step 3 Code Directory: Chapter9/Step3 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java
  • 39. Step 3 Test We run the program against varying types of input textfiles. We can use a long document such as the term paper for the last term’s economy class (don’t forget to save it as a textfile before testing). We should also use some specially created files for testing purposes. One file may contain one word repeated 7 times, for example. Another file may contain no words at all.
  • 40. Step 4: Finalize Possible Extensions One is an integrated user interface where the end user can view both the input document files and the output word list files. Another is the generation of different types of list. In the sample development, we count the number of occurences of each word. Instead, we can generate a list of positions where each word appears in the document.

Editor's Notes

  • #3: We will cover the basic string processing in this lesson, manipulating char and String data.
  • #5: 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.
  • #6: 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.
  • #8: We introduced the String class in Chapter 2. We will study additional String methods.
  • #10: 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;);
  • #11: 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.
  • #12: 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
  • #13: 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.
  • #14: 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.
  • #15: Here are some examples of regular expressions.
  • #19: 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.
  • #21: 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.
  • #22: If the situation calls for directing changing the contents of a string, then we use the StringBuffer class.
  • #23: 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.
  • #24: 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.
  • #25: The append and insert are the two very useful methods of the StringBuffer class for string manipulation.
  • #28: 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.
  • #29: There will be a total of six key classes in this application. We will be designing two classes: Ch9WordConcordanceMain and Ch9WordConcordance.
  • #33: Please use your Java IDE to view the source files and run the program.
  • #41: 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.
  翻译: