String Processing 9-17-2002. Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
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.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
11-Jun-15 Using the Java API
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 ,
Using the Java API
Fundamental Programming Structures in Java: Strings.
29-Jun-15 Using the Java API
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Intro to C++ And Some Tools Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project.
Recitation 2 Main Method, API & Packages, Java Basics.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Do-while and for Loops Opening Discussion zWhat did we talk about last class? zAt the end of last class I asked you to write a loop that would.
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? Don’t let problems hang.
10-Nov-15 Java Object Oriented Programming What is it?
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Threads Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment?
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
More Inheritance and More Java Stuff Opening Discussion zWhat did we talk about last class? zHave you read the description of assignment #2?
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Stack, Queues, and Priority Queues: Linked List Based
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,
The Methods and What You Need to Know for the AP Exam
More Sophisticated Behavior
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Java Programming: Guided Learning with Early Objects
Chapter 10 Thinking in Objects
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Lecture 07 String Jaeki Song.
Chapter 10 Thinking in Objects Part 2
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Unit-2 Objects and Classes
Presentation transcript:

String Processing

Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions about assignment #2? zIf you are interested, the ACM programming team meetings are Thursday’s at 4:30. Send me an if you want to be on the list.

Comments about Assignment #2 zThe design for assignment #2 is due on Thursday. Again I would like for you to generate the HTML documentation and post that then send me a link. zIt needs to include your screen implementation and the implementations of the main blocks that you will use. You can add this onto the project you did for assignment #1 or create a new one, but you will need a class with main in it. zI would like for you to tags in the javadoc comments for methods.

Coding for Assignment #2 zWhen you write the code you need to construct your screen and block classes. You can use the BasicBlock code to help with your blocks. You also need to edit the GameSetup code to that it uses your version of Screen instead of BasicScreen. zLet’s look at the Screen interface to see what methods you need to create.

The String Class zOne of the most fundamental types in any real programming language is that of a string of characters. There is a String class in java.lang that fills this role for Java. zToday we are going to look at that class and what it can do. I want you to bring up the JavaDoc for it in the Sun API web page.

Constructors zLooking at the JavaDocs, you can see that there are many different constructors for String including a copy constructor and versions that create Strings from arrays of chars and bytes. zThere is also a constructor that creates a String from an object of type StringBuffer. We’ll discuss the StringBuffer class a bit later.

Notes on Immutability zThe String class is immutable. As a result, once the constructor has been invoked, none of the methods will change the value of that string. zSome methods might look like they would change the value, but what they actually do is return a new String object with the appropriate alterations.

Concatenation, the Overloaded ‘+’ zJava doesn’t allow you to overload operators and doesn’t overload many operators itself. One exception to this was the decision to allow the ‘+’ operator to do string concatenation. zIn addition to being able to concatenate strings, you can use it with a string and a numeric type or an Object. More on this in two slides.

Indexing into Strings zLike most things in C-family languages, Strings in Java are zero referenced. So the elements of a String str have indexes between 0 and str.length()-1, inclusive. zThis indexing is used for methods like charAt, indexOf, substring, and others. zNote that the two argument substring specifies that the second index is the one AFTER the last character you want.

Conversions to String zThe String class has a number of static methods with the name valueOf. These take different argument types, and return strings that represent the given arguments. zFor the primitive types you can sometimes get more control over these types of conversions using the methods provided in their wrapper classes.

The StringBuffer Class zIf you have a “String” that you need to change the value of on a regular basis, you probably want to use an instance of StringBuffer instead of String. zGo the the page for the StringBuffer API. Note that the methods like append and insert return a StringBuffer making one thing they behave similar to String. Read the description of the return parameter to see this is not the case.

Identity vs. Equality zWhile the ‘+’ operator has been overloaded for Strings, the ‘==‘ operator hasn’t. This operator checks for equality of references, not the contents of what they reference. zTo check for equality of the string contents you need to use the equals() method. There is also a compareTo method that can be used, but it doesn’t return a boolean.

StringTokenizer zThe java.util package has a class called StringTokenizer that can be quite helpful when you want to parse a string into pieces. zYou construct it with a String to be tokenized and an optional list of separators. It then lets you pull off tokens one at a time.

Let’s Code Some zNow we are going to play around some more with the code we started last class and see where we might be able to use Strings in it.

Minute Essay zWrite me a small method with the following signature:  int countOccur(String s,char c); zThis method should return how many times the given character occurs in the String. zRemember that the design for assignment #2 is due on Thursday. Also read chapter 8 for Thursday’s class.