Strings Testing for equality with strings.

Slides:



Advertisements
Similar presentations
Java
Advertisements

Strings in Java 1. strings in java are handled by two classes String &
Logic & program control part 2: Simple selection structures.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Comparing Data Comparing More than Numbers. Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of.
Fundamental Programming Structures in Java: Strings.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Lists and More About Strings CS303E: Elements of Computers and Programming.
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Strings.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Decision Structures, String Comparison, Nested Structures
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Jordan Jozwiak CS50. Announcements Pset3 will be returned by 7pm on Tuesday REMINDER: Access section materials from this year and last year at
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
 Type Called bool  Bool has only two possible values: True and False.
Java String Methods - Codehs
CprE 185: Intro to Problem Solving (using C)
Strings, StringBuilder, and Character
Jordi Cortadella Department of Computer Science
Chapter 3 Edited by JJ Shepherd
Chapter 3 Branching Statements
Multiple variables can be created in one declaration
String Objects & its Methods
Decision Structures, String Comparison, Nested Structures
Chapter 7: Strings and Characters
Logical Operators & Truth Tables.
Decision Structures, String Comparison, Nested Structures
The compareTo interface
The Data Element.
Flow of Control Chapter 3.
The Data Element.
Each object belongs to a class
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Boolean Expressions September 1, 2019 ICS102: The course.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
String Methods Strings have actions known as method. We will review a few of the methods associated with strings that are a part of the built in Java.
Presentation transcript:

Strings Testing for equality with strings. Lexicographic ordering of strings. Other string methods.

Strings and Testing for Equality The == operator is also not appropriate for determining if two objects, such as strings, have the same value. String s1 = “hello”; String s2 = “hello”; : if (s1 == s2) System.out.println(“Strings are equal!”); else System.out.println(“Strings are not equal!”); The above code does not compare the strings in s1 and s2; why?

Strings and Testing for Equality To test the equality of objects of class String, use method equals: String s1 = “hello”; String s2 = “hello”; if (s1.equals(s2)) System.out.println(“Strings are equal!”); else System.out.println(“Strings are not equal!”); Could have used s2.equals(s1) in the above.

Strings and Testing for Equality Another example: String s1 = “dog”; String s2 = “cat”; if (s1 == s2) System.out.println(“Strings are equal!”); // Correct answer, but else //for the wrong reason!!! System.out.println(“Strings are not equal!”); s1 = s2; // What happens to the memory used by s1? System.out.println(s1); // What is output here? System.out.println(“Strings are equal!”); else => “a CAT, is NOT, a DOG!” (from the musical Cats) ≠

Strings and Testing for Equality Note that equals is case-sensitive! (what does that mean?) Normally these methods are called on variables, but not always: s1.equals(s2) // The typical case s1.equals(“dog”) // Also very common “dog”.equals(s1) // Unusual, but occasionally used “dog”.equals(“cat”) // Least likely To test for equality ignoring case, use equalsIgnoreCase: if (s1.equalsIgnoreCase(“HelLo”)) would evaluate to true if s1 contained “hello”

Strings and Testing for Equality

Some (odd) Terminology Get ready, because “this” is going to be a bit strange… Note on terminology – method calls in Java take the following form: object.method(parameter_1, parameter_2,…,parameter_N) In such cases object is frequently referred to as “this.” Example: equals(Other_String) – Returns true if this string and Other_String are equal. Otherwise, returns false. s1.equals(s2) – Here, s1 is “this.”

Lexicographic Order Lexicographic order is similar to alphabetical order. Based on the order of all the ASCII and Unicode characters: All digits come before all the letters. All uppercase letters come before all lower case letters. The same as alphabetical ordering when all characters are either upper or lower, but not when upper and lower case are mixed. Zip comes before apple Sorting and ordering are really big deals, both algorithmically and historically (check the wikipedia).

Lexicographic Order Some additional string methods: toUpperCase() – Returns a new string having the same characters as this string, but with any lowercase letters converted to uppercase. toLowerCase() - Returns a new string having the same characters as this string, but with any uppercase letters converted to lowercase. compareTo(A_String) – Compares this string with A_String to see which string comes first in lexicographic ordering. Returns a negative integer if this string is first, returns zero if the two strings are equal, and returns a positive integer if A_String is first.

Lexicographic Order Example: String s1 = “Hello”; String s2 = s1.toLowerCase(); String s3 = “hello”; if (s2.compareTo(s3) == 0) System.out.println(“Equal!”); else if (s2.compareTo(s3) < 0) System.out.println(“s2 comes first lexicographically!”); else System.out.println(“s3 comes first lexicographically!”);

Lexicographic Order Example: String s1 = “Hello”; String s2 = “dog”; if (s2.compareTo(s3) == 0) System.out.println(“Equal!”); else if (s2.compareTo(s3) < 0) System.out.println(“s2 comes first lexicographically!”); else System.out.println(“s3 comes first lexicographically!”);

Lexicographic Order Example: String s1 = “Hello”; String s2 = “dog”; if (s2.compareTo(s3) == 0) System.out.println(“Equal!”); else if (s2.compareTo(s3) < 0) System.out.println(“s2 comes first lexicographically!”); else System.out.println(“s3 comes first lexicographically!”);

Lexicographic Order Example ignoring upper and lower case: //or use s1.compareToIgnoreCase(s2) String s1 = “Hello”; String s2 = “hello”; if (s1.compareToIgnoreCase(s2) == 0) System.out.println(“Equal!”); else if (s1.compareToIgnoreCase(s2) < 0) System.out.println(“s1 comes first lexicographically!”); else System.out.println(“s2 comes first lexicographically!”);