Java căn bản - Chapter9

Vince Vo
Vince VoSoftware Developer en Freelancer
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]
1 de 40

Recomendados

M C6java7 por
M C6java7M C6java7
M C6java7mbruggen
487 vistas51 diapositivas
M C6java2 por
M C6java2M C6java2
M C6java2mbruggen
277 vistas28 diapositivas
Strings v.1.1 por
Strings v.1.1Strings v.1.1
Strings v.1.1BG Java EE Course
2.5K vistas62 diapositivas
String in python lecture (3) por
String in python lecture (3)String in python lecture (3)
String in python lecture (3)Ali ٍSattar
218 vistas9 diapositivas
Python strings por
Python stringsPython strings
Python stringsMohammed Sikander
2.1K vistas36 diapositivas
C# String por
C# StringC# String
C# StringRaghuveer Guthikonda
3K vistas12 diapositivas

Más contenido relacionado

La actualidad más candente

M C6java3 por
M C6java3M C6java3
M C6java3mbruggen
249 vistas24 diapositivas
Strings por
StringsStrings
StringsMitali Chugh
5.6K vistas24 diapositivas
Strings in C por
Strings in CStrings in C
Strings in CKamal Acharya
24.1K vistas22 diapositivas
Strings in Python por
Strings in PythonStrings in Python
Strings in Pythonnitamhaske
1.3K vistas36 diapositivas
The string class por
The string classThe string class
The string classSyed Zaid Irshad
1.9K vistas29 diapositivas
String.ppt por
String.pptString.ppt
String.pptajeela mushtaq
2.7K vistas19 diapositivas

La actualidad más candente(20)

M C6java3 por mbruggen
M C6java3M C6java3
M C6java3
mbruggen249 vistas
Strings in Python por nitamhaske
Strings in PythonStrings in Python
Strings in Python
nitamhaske1.3K vistas
Ap Power Point Chpt2 por dplunkett
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
dplunkett566 vistas
Strings por Imad Ali
StringsStrings
Strings
Imad Ali1.9K vistas
Java: Regular Expression por Masudul Haque
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
Masudul Haque1.8K vistas
String Handling por Bharat17485
String HandlingString Handling
String Handling
Bharat174854.9K vistas
16 Java Regex por wayn
16 Java Regex16 Java Regex
16 Java Regex
wayn4.7K vistas
Ap Power Point Chpt4 por dplunkett
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
dplunkett515 vistas
strings por teach4uin
stringsstrings
strings
teach4uin391 vistas

Destacado

Rama Ch9 por
Rama Ch9Rama Ch9
Rama Ch9Vince Vo
386 vistas45 diapositivas
Rama Ch4 por
Rama Ch4Rama Ch4
Rama Ch4Vince Vo
338 vistas56 diapositivas
Rama Ch5 por
Rama Ch5Rama Ch5
Rama Ch5Vince Vo
279 vistas18 diapositivas
Java căn bản - Chapter2 por
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2Vince Vo
322 vistas51 diapositivas
Java căn bản - Chapter7 por
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
434 vistas46 diapositivas
Java căn bản - Chapter12 por
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12Vince Vo
458 vistas37 diapositivas

Destacado(20)

Rama Ch9 por Vince Vo
Rama Ch9Rama Ch9
Rama Ch9
Vince Vo386 vistas
Rama Ch4 por Vince Vo
Rama Ch4Rama Ch4
Rama Ch4
Vince Vo338 vistas
Rama Ch5 por Vince Vo
Rama Ch5Rama Ch5
Rama Ch5
Vince Vo279 vistas
Java căn bản - Chapter2 por Vince Vo
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2
Vince Vo322 vistas
Java căn bản - Chapter7 por Vince Vo
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
Vince Vo434 vistas
Java căn bản - Chapter12 por Vince Vo
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
Vince Vo458 vistas
Java căn bản - Chapter3 por Vince Vo
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
Vince Vo549 vistas
Rama Ch7 por Vince Vo
Rama Ch7Rama Ch7
Rama Ch7
Vince Vo303 vistas
Rama Ch1 por Vince Vo
Rama Ch1Rama Ch1
Rama Ch1
Vince Vo398 vistas
Rama Ch14 por Vince Vo
Rama Ch14Rama Ch14
Rama Ch14
Vince Vo304 vistas
Rama Ch10 por Vince Vo
Rama Ch10Rama Ch10
Rama Ch10
Vince Vo302 vistas
Java căn bản - Chapter13 por Vince Vo
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
Vince Vo464 vistas
Rama Ch3 por Vince Vo
Rama Ch3Rama Ch3
Rama Ch3
Vince Vo395 vistas
Hướng dẫn cài đặt Java por Vince Vo
Hướng dẫn cài đặt JavaHướng dẫn cài đặt Java
Hướng dẫn cài đặt Java
Vince Vo4.3K vistas
Rama Ch11 por Vince Vo
Rama Ch11Rama Ch11
Rama Ch11
Vince Vo299 vistas
Rama Ch6 por Vince Vo
Rama Ch6Rama Ch6
Rama Ch6
Vince Vo408 vistas
Rama Ch12 por Vince Vo
Rama Ch12Rama Ch12
Rama Ch12
Vince Vo236 vistas
Java căn bản - Chapter4 por Vince Vo
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
Vince Vo435 vistas
Rama Ch8 por Vince Vo
Rama Ch8Rama Ch8
Rama Ch8
Vince Vo432 vistas
Hiscox "la maison de demain" por NOVABUILD
Hiscox "la maison de demain"Hiscox "la maison de demain"
Hiscox "la maison de demain"
NOVABUILD3.8K vistas

Similar a Java căn bản - Chapter9

Strings Arrays por
Strings ArraysStrings Arrays
Strings Arraysphanleson
1.6K vistas39 diapositivas
The JavaScript Programming Language por
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
1.4K vistas142 diapositivas
The Java Script Programming Language por
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
983 vistas142 diapositivas
Javascript by Yahoo por
Javascript by YahooJavascript by Yahoo
Javascript by Yahoobirbal
740 vistas142 diapositivas
Les origines de Javascript por
Les origines de JavascriptLes origines de Javascript
Les origines de JavascriptBernard Loire
436 vistas142 diapositivas
Javascript por
JavascriptJavascript
Javascriptguest03a6e6
20K vistas142 diapositivas

Similar a Java căn bản - Chapter9(20)

Strings Arrays por phanleson
Strings ArraysStrings Arrays
Strings Arrays
phanleson1.6K vistas
The JavaScript Programming Language por Raghavan Mohan
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan1.4K vistas
The Java Script Programming Language por zone
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone983 vistas
Javascript by Yahoo por birbal
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal740 vistas
Les origines de Javascript por Bernard Loire
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Bernard Loire436 vistas
OCA Java SE 8 Exam Chapter 3 Core Java APIs por İbrahim Kürce
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ürce2.6K vistas
String and string buffer por kamal kotecha
String and string bufferString and string buffer
String and string buffer
kamal kotecha5.6K vistas
String interpolation por Knoldus Inc.
String interpolationString interpolation
String interpolation
Knoldus Inc.1.2K vistas

Más de Vince Vo

Java căn bản - Chapter10 por
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
693 vistas56 diapositivas
Java căn bản - Chapter8 por
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8Vince Vo
404 vistas42 diapositivas
Java căn bản - Chapter6 por
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
653 vistas52 diapositivas
Java căn bản - Chapter5 por
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5Vince Vo
815 vistas56 diapositivas
Java căn bản- Chapter1 por
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1Vince Vo
474 vistas19 diapositivas
Rama Ch13 por
Rama Ch13Rama Ch13
Rama Ch13Vince Vo
374 vistas24 diapositivas

Más de Vince Vo(8)

Java căn bản - Chapter10 por Vince Vo
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo693 vistas
Java căn bản - Chapter8 por Vince Vo
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8
Vince Vo404 vistas
Java căn bản - Chapter6 por Vince Vo
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo653 vistas
Java căn bản - Chapter5 por Vince Vo
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5
Vince Vo815 vistas
Java căn bản- Chapter1 por Vince Vo
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1
Vince Vo474 vistas
Rama Ch13 por Vince Vo
Rama Ch13Rama Ch13
Rama Ch13
Vince Vo374 vistas
Rama Ch12 por Vince Vo
Rama Ch12Rama Ch12
Rama Ch12
Vince Vo216 vistas
Rama Ch2 por Vince Vo
Rama Ch2Rama Ch2
Rama Ch2
Vince Vo367 vistas

Último

Classification of crude drugs.pptx por
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptxGayatriPatra14
104 vistas13 diapositivas
Jibachha publishing Textbook.docx por
Jibachha publishing Textbook.docxJibachha publishing Textbook.docx
Jibachha publishing Textbook.docxDrJibachhaSahVetphys
53 vistas14 diapositivas
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant... por
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...Ms. Pooja Bhandare
166 vistas45 diapositivas
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023 por
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023A Guide to Applying for the Wells Mountain Initiative Scholarship 2023
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023Excellence Foundation for South Sudan
69 vistas26 diapositivas
A-Level Art por
A-Level ArtA-Level Art
A-Level ArtWestHatch
48 vistas82 diapositivas
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx por
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxGopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxDebapriya Chakraborty
709 vistas81 diapositivas

Último(20)

Classification of crude drugs.pptx por GayatriPatra14
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptx
GayatriPatra14104 vistas
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant... por Ms. Pooja Bhandare
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Ms. Pooja Bhandare166 vistas
A-Level Art por WestHatch
A-Level ArtA-Level Art
A-Level Art
WestHatch48 vistas
Class 9 lesson plans por TARIQ KHAN
Class 9 lesson plansClass 9 lesson plans
Class 9 lesson plans
TARIQ KHAN53 vistas
GCSE Media por WestHatch
GCSE MediaGCSE Media
GCSE Media
WestHatch48 vistas
CUNY IT Picciano.pptx por apicciano
CUNY IT Picciano.pptxCUNY IT Picciano.pptx
CUNY IT Picciano.pptx
apicciano56 vistas
Retail Store Scavenger Hunt.pptx por jmurphy154
Retail Store Scavenger Hunt.pptxRetail Store Scavenger Hunt.pptx
Retail Store Scavenger Hunt.pptx
jmurphy15447 vistas
How to empty an One2many field in Odoo por Celine George
How to empty an One2many field in OdooHow to empty an One2many field in Odoo
How to empty an One2many field in Odoo
Celine George97 vistas
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx por ISSIP
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptxEIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
ISSIP407 vistas
GCSE Music por WestHatch
GCSE MusicGCSE Music
GCSE Music
WestHatch47 vistas
Relationship of psychology with other subjects. por palswagata2003
Relationship of psychology with other subjects.Relationship of psychology with other subjects.
Relationship of psychology with other subjects.
palswagata200377 vistas

Java căn bản - Chapter9

  • 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.