©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 9 - 1 Chapter 9 Characters and Strings (adapted from.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 9 Characters and Strings.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Java Programming Strings Chapter 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 6, 15) Recursive Data Structures.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism Animated Version.
Intro to OOP with Java, C. Thomas Wu Characters and Strings
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 8 Characters and Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
JavaScript, Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Characters and Strings. Representation of single characters Data type char is the data type that represents single characters, such as letters, numerals,
Chapter 8: String Manipulation
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9 Characters and Strings.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
Characters In Java single characters are represented using the data type char. Character constants are written as symbols enclosed in single quotes, for.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Input, Output, and Processing
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
17-Feb-16 String and StringBuilder Part I: String.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Inheritance and Polymorphism.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
More About Objects and Methods
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Chapter 3 : Basic Concept Of Java
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
Chapter 7: Strings and Characters
Chapter 2: Basic Elements of Java
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Chapter 10 Thinking in Objects Part 2
Getting Started with Java
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (adapted from the publisher’s slides) Animated Version

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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter II. Characters Animated Version

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter ASCII Encoding For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Character Processing Declaration and initialization char ch1, ch2 = ‘X’; Type conversion between int and char. System.out.print("ASCII code of character X is " + (int) ' X ' ); System.out.print("Character with ASCII code 88 is " + (char)88 ); This comparison returns true because ASCII value of 'A' is 65 while that of 'c' is 99. ‘A’ < ‘c’

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter III. Strings and Regular Expression Animated Version

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Accessing Individual Elements Individual characters in a String accessed with the charAt method Sumatr a String name = "Sumatra"; name This variable refers to the whole string. name.charAt( 3 ) The method returns the character at position # 3.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Example: Counting Vowels char letter; String name = JOptionPane.showInputDialog(null,"Your name:"); int numberOfCharacters = name.length(); int vowelCount= 0; for (int i = 0; i < numberOfCharacters; i++) { letter = name.charAt(i); if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i' || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U' ) { vowelCount++; } System.out.print(name + ", your name has " + vowelCount + " vowels"); Here’s the code to count the number of vowels in the input string.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Example: Counting ‘Java’ int javaCount = 0; boolean repeat = true; String word; while ( repeat ) { word = JOptionPane.showInputDialog(null,"Next word:"); if ( word.equals("STOP") ) { repeat = false; } else if ( word.equalsIgnoreCase("Java") ) { javaCount++; } Continue reading words and count how many times the word Java occurs in the input, ignoring the case. Notice how the comparison is done. We are not using the == operator.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Other Useful String Operators MethodMeaning compareTo Compares the two strings. str1.compareTo( str2 ) substring Extracts the a substring from a string. str1.substring( 1, 4 ) trim Removes the leading and trailing spaces. str1.trim( ) valueOf Converts a given primitive data value to a string. String.valueOf( ) startsWith Returns true if a string starts with a specified prefix string. str1.startsWith( str2 ) endsWith Returns true if a string ends with a specified suffix string. str1.endsWith( str2 ) See the String class documentation for details.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Regular Expression Examples ExpressionDescription [013] A single digit 0, 1, or 3. [0-9][0-9] Any two-digit number from 00 to 99. [0-9&&[^4567]] A single digit that is 0, 1, 2, 3, 8, or 9. [a-z0-9] A single character that is either a lowercase letter or a digit. [a-zA-z][a-zA-Z0-9_$]* A valid Java identifier consisting of alphanumeric characters, underscores, and dollar signs, with the first character being an alphabet. [wb](ad|eed) Matches wad, weed, bad, and beed. (AZ|CA|CO)[0-9][0-9] Matches AZxx, CAxx, and COxx, where x is a single digit.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The replaceAll Method The replaceAll method replaces all occurrences of a substring that matches a given regular expression with a given replacement string. String originalText, modifiedText; originalText =...; //assign string modifiedText = Replace all vowels with the

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter IV. StringBuffer Class (and StringBuilder Class) Animated Version

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Effect of Immutability We can do this because String objects are immutable.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter StringBuffer Example StringBuffer word = new StringBuffer("Java"); word.setCharAt(0, 'D'); word.setCharAt(1, 'i'); Changing a string Java to Diva word : StringBuffer Java Before word : StringBuffer Diva After word.setCharAt(0, 'D'); word.setCharAt(1, 'i');

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sample Processing Replace all vowels in the sentence with ‘X’. char letter; String inSentence = JOptionPane.showInputDialog(null, "Sentence:"); 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 McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter V. Sample Development Animated Version

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Overall Plan Tasks expressed in pseudocode: while ( the user wants to process another file ) { Task 1: read the file; Task 2: build the word list; Task 3: save the word list to a file; }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Design Document ClassPurpose Ch9WordConcordanceMainThe instantiable main class of the program that implements the top-level program control. Ch9WordConcordanceThe key class of the program. An instance of this class managers other objects to build the word list. FileManagerA helper class for opening a file and saving the result to a file. Details of this class can be found in Chapter 12. WordListAnother helper class for maintaining a word list. Details of this class can be found in Chapter 10. Pattern/MatcherClasses for pattern matching operations.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Class Relationships FileManger Ch9Word Concordance Matcher WordList Pattern Ch9Word ConcordanceMain (main class) class we implement helper class given to us standard class

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Development Steps We will develop this program in four steps: 1.Start with a program skeleton. Define the main class with data members. Begin with a rudimentary Ch9WordConcordance class. 2.Add code to open a file and save the result. Extend the existing classes as necessary. 3.Complete the implemention of the Ch9WordConcordance class. 4.Finalize the code by removing temporary statements and tying up loose ends.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Step 1 Design Define the skeleton main class Define the skeleton Ch9WordConcordance class that has only an empty zero-argument constructor

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Step 1 Code Directory: Chapter9/Step1 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Step 2 Code Directory: Chapter9/Step2 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java Directory: Chapter9/Step2 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Step 3 Code Directory: Chapter9/Step3 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java Directory: Chapter9/Step3 Source Files: Ch9WordConcordanceMain.java Ch9WordConcordance.java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.