Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods

Slides:



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

String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
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 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Fundamental Programming Structures in Java: Strings.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
Introduction to Java Java Translation Program Structure
Chapter 7: Characters, Strings, and the StringBuilder.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
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.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
String Definition A string is a set of characters that behaves as a single unit. The characters in a string include upper-case and lower- case letters,
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:
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.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Chapter VII: Arrays.
Section 6.1 Introduction to String Methods. Section 6.1 Introduction to String Methods.
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
PowerPoint Presentation Authors of Exposure Java
Building Java Programs
String class.
Multiple variables can be created in one declaration
Primitive Types Vs. Reference Types, Strings, Enumerations
Exposure Java 2015 AP®CS Edition Chapter 8 Slides Manipulating Strings
String Objects & its Methods
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
An Introduction to Java – Part I, language basics
Chapter 9 Strings and Text I/O
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Java – String Handling.
Lecture 07 String Jaeki Song.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
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.
In Java, strings are objects that belong to class java.lang.String .
Pre-AP® Computer Science Quiz
Unit-2 Objects and Classes
Presentation transcript:

Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods PowerPoint Presentation created by: Mr. John L. M. Schram and Mr. Leon Schram Authors of Exposure Java

Section 12.1 Introduction

String Processing Word processing term papers, writing memoirs, sending email messages, responding to surveys, placing online orders and registering products all involve string processing. Every software package on the market includes string-processing components. Every programming language has special features that facilitate the manipulation of strings, and Java is no different.

String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters, numerical characters and a large set of characters for a variety of purposes like: ! @ # $ % ^ & * ( ) _ +

String Variables vs. String Literals A string literal is a set of characters delimited with double quotations. String name = “John Smith”; name is the string variable. “John Smith” is the string literal.

Section 12.2 Constructing String Objects

Is String a Simple Data Type? When you see statements like: which looks very similar to you might get the idea that String is a simple (or primitive) data type like int, double, char, and boolean. However, String is NOT a simple data type. String is a class. String name = “John Smith”; int x = 5;

Java1201.java s1: Tango s2: Tango s3: Tango s4: Tango s5: Tango // This program demonstrates multiple ways to construct String objects.   public class Java1201 { public static void main (String args[]) System.out.println("\nJava1201.java\n"); String s1 = "Tango"; System.out.println("s1: " + s1); String s2 = new String(); // Default constructor s2 = "Tango"; System.out.println("s2: " + s2); String s3 = new String("Tango"); // Overloaded constructor with String parameter System.out.println("s3: " + s3); String s4 = new String(s3); // Same constructor as s3 System.out.println("s4: " + s4); char dance[ ] = {'T','a','n','g','o'}; String s5 = new String(dance); // Overloaded constructor with char array parameter System.out.println("s5: " + s5); System.out.println(); } Java1201.java s1: Tango s2: Tango s3: Tango s4: Tango s5: Tango

Section 12.3 String Concatenation

Mathematical Addition 100 + 200 = 300 int x = 100; x += 200; x now stores 300. When used with int or double the plus sign ( + ) performs addition.

String Concatenation When used with Strings the plus “100” + “200” = “100200” String x = “100”; x += “200”; x now stores “100200”. When used with Strings the plus sign ( + ) performs String Concatenation.

Java1202.java s3: Argentine Tango s6: 10002000 // Java1202.java // This program shows how to concatenate strings using the + operator. public class Java1202 { public static void main (String args[]) System.out.println("\nJava1202.java\n"); String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println("s3: " + s3); String s4 = "1000"; String s5 = "2000"; String s6 = s4 + s5; System.out.println("s6: " + s6); System.out.println(); } Java1202.java s3: Argentine Tango s6: 10002000

Section 12.4 Working with Substrings

Do not confuse the length() method of String // Java1203.java // This program demonstrates the use of the <length> method. public class Java1203 { public static void main (String args[]) System.out.println("\nJava1203.java\n"); String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println(s1 + " has " + s1.length() + " characters."); System.out.println(s2 + " has " + s2.length() + " characters."); System.out.println(s3 + " has " + s3.length() + " characters."); System.out.println(); } Java1203.java Argentine has 9 characters. Tango has 5 characters. Argentine Tango has 15 characters. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 A r g e n t i T a o Do not confuse the length() method of String with the length field of a Java static Array!

// This program demonstrates that in Java a <String> // Java1204.java // This program demonstrates that in Java a <String> // is NOT an Array of Characters. public class Java1204 { public static void main (String args[]) System.out.println("\nJava1204.java\n"); String state = "Texas"; System.out.println( state[2] ); System.out.println(); } 1 2 3 4 T e x a s

Are Strings Really Arrays of Characters? In many computer languages, like C++, you can access the individual characters in a string by using the index[ ] operator. That is because in those languages, a string is implemented as an array of characters. In Java this is NOT the case. String is a class and like most classes, the String class has methods. One of them is charAt which is what you use to access a character At a specific index.

// This program shows the proper way to access an individual // Java1205.java // This program shows the proper way to access an individual // character in a <String> using the <charAt> method. public class Java1205 { public static void main (String args[]) System.out.println("\nJava1205.java\n"); String state = "Texas"; System.out.println( state.charAt(2) ); System.out.println(); } 1 2 3 4 T e x a s Java1205.java x

String Method charAt String s = "Texas"; int k = 2; char letter1 = s.charAt(k); char letter2 = s.charAt(2); 1 2 3 4 T e x a s Method charAt returns the character stored at the integer index location of the string object s. The index parameter can be an integer variable or an integer constant. The first character is at index 0.   In this case, both letter1 and letter2 become 'x'.

for (int k = n; k >= 0; k--) s2 += s1.charAt(k); s1 M a d m I ' A // Java1206.java // This program demonstrates how to create one string object that is the // reverse of another. The <charAt> method is used inside a <for> loop. public class Java1206 { public static void main (String args[]) System.out.println("\nJava1206.java\n"); String s1 = "Madam I'm Adam"; String s2 = ""; int n = s1.length() - 1; for (int k = n; k >= 0; k--) s2 += s1.charAt(k); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 s1 M a d m I ' A s2 Java1206.java s1: Madam I'm Adam s2: madA m'I madaM

R a c e r Java1207.java Race ace ce ceca eca car // Java1207.java // This program demonstrates how to access specified characters of a string // with the <substring(SI,EI)> method, where SI is the Starting Index and // EI is one more than the Ending Index. public class Java1207 { public static void main (String args[]) System.out.println("\nJava1207.java\n"); String s = "Racecar"; System.out.println(s.substring(0,4)); System.out.println(s.substring(1,4)); System.out.println(s.substring(2,4)); System.out.println(s.substring(2,6)); System.out.println(s.substring(3,6)); System.out.println(s.substring(4,7)); System.out.println(); } 1 2 3 4 5 6 R a c e r Java1207.java Race ace ce ceca eca car

String Method substring s1 = “Aardvark”; s2 = s1.substring(j,k); Method substring returns a set of consecutive characters from String s1, starting at index j, and ending at index k-1. s3 = s1.substring(4,7); s3 becomes "var" s1 1 2 3 4 5 6 7 A a r d v k

s1 r a c e s2 r a c e i n t h p o int index1 = s1.indexOf(s3); Java1208.java With "racecar" car starts at 4 and last shows up at 4 With "racecar in the carport" car starts at 4 and last shows up at 15 With "car" Qwerty shows up at -1 // Java1208.java // This program shows the <indexOf> method, which returns the index of the first // occurrence of the string argument, and the <lastIndexOf> method, which returns the // index of the last occurrence of the string argument. public class Java1208 { public static void main (String args[]) System.out.println("\nJava1206.java\n"); String s1 = "racecar"; String s2 = "racecar in the carport"; String s3 = "car"; int index1 = s1.indexOf(s3); int index2 = s1.lastIndexOf(s3); int index3 = s2.indexOf(s3); int index4 = s2.lastIndexOf(s3); int index5 = s1.indexOf("qwerty"); System.out.println("With \"" + s1 + "\" car starts at " + index1 + " and last shows up at " + index2); System.out.println("With \"" + s2 + "\" car starts at " + index3 + " and last shows up at " + index4); System.out.println("With \"" + s3 + "\" Qwerty shows up at " + index5); System.out.println(); } s1 1 2 3 4 5 6 r a c e s2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 r a c e i n t h p o

String Methods indexOf & lastIndexOf indexOf returns the first occurrence of a substring s1.indexOf(“hum”); returns 0 s1.indexOf(“ku”); returns 10 s1.indexOf(“qwerty”); returns -1 lastIndexOf returns the last occurrence of a substring s1.lastIndexOf(“hum”); returns 4 s1.lastIndexOf(“ku”); returns 14 s1.lastIndexOf(“qwerty”); returns -1 Note: The i in indexOf is lowercase, but in lastIndexOf it’s capital! s1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 h u m n k a p ’ By the way, it is the State Fish of Hawaii.

Section 12.5 Changing Strings

Java1209.java s1: racecar s2: lacecal // This program demonstrates the <replace(Old,New)> String // method, which replaces all occurrences of the Old character with // the New character. This method creates a new String object with // the replaced characters. public class Java1209 { public static void main (String args[]) System.out.println("\nJava1209.java\n"); String s1 = "racecar"; String s2 = s1.replace('r','l'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); } Java1209.java s1: racecar s2: lacecal NOTE: The replace method does NOT alter s1 AT ALL. It creates a copy of the string which is returned and stored in s2.

String Method replace String s2 = s1.replace('m','b'); Method replace returns a string such that every occurrence of the old character 'm' is replaced by the new character 'b' . If s1 == "madam" then s2 becomes "badab".

// Java1210.java // This program demonstrates the String <toUpperCase> and // <toLowerCase> methods. public class Java1210 { public static void main (String args[]) System.out.println("\nJava1210.java\n"); String s1 = "racecar"; String s2 = "RaCeCaR"; String s3 = "RACECAR100"; String s4 = s1.toUpperCase(); String s5 = s2.toUpperCase(); String s6 = s3.toUpperCase(); String s7 = s1.toLowerCase(); String s8 = s2.toLowerCase(); String s9 = s3.toLowerCase(); System.out.println("\nOriginal Strings"); System.out.println("----------------"); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3);

System.out.println("\nStrings Converted to All Uppercase"); System.out.println("s4: " + s4); System.out.println("s5: " + s5); System.out.println("s6: " + s6); System.out.println("\nStrings Converted to All Lowercase"); System.out.println("s7: " + s7); System.out.println("s8: " + s8); System.out.println("s9: " + s9); // This is what you need to do to alter the original strings. s1 = s1.toUpperCase(); s2 = s2.toUpperCase(); s3 = s3.toLowerCase(); System.out.println("\nOriginal Strings Again"); System.out.println("----------------------"); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println(); }

Java1210.java Original Strings ---------------- s1: racecar s2: RaCeCaR s3: RACECAR100 Strings Converted to All Uppercase ---------------------------------- s4: RACECAR s5: RACECAR s6: RACECAR100 Strings Converted to All Lowercase s7: racecar s8: racecar s9: racecar100 Original Strings Again ---------------------- s1: RACECAR s2: RACECAR s3: racecar100

String Methods toUpperCase and toLowerCase s1 = s2.toUpperCase(); s3 = s1.toLowerCase(); Method toUpperCase returns a String where all letters are upper-case. Method toLowerCase returns a String where all letters are lower-case. Any characters that are not letters will be ignored by both methods and returned in their same relative String position.

Altering the Original String Remember, String methods do not alter the original String object. They return an altered copy of the String object. To alter the original String object, you need a statement that assigns the new copy back to the original object.   Examples: s1 = s1.toUpperCase(); s2 = s2.toLowerCase(); s3 = s3.replace('m','b'); s4 = s4.substring(1,5);

Section 12.6 Converting Strings

Java1211.java s1: 1000 s2: 123.321 s3: true s4: A s5: 1000123.321 // This program demonstrates the <valueOf> method of the String class, // which is shown to convert four data types to a string. // Note that <valueOf> is a static method and must be called using <String.valueOf>. public class Java1211 { public static void main (String args[]) System.out.println("\nJava1211.java\n"); String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); String s5 = s1 + s2; System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println("s4: " + s4); System.out.println("s5: " + s5); System.out.println(); } Java1211.java s1: 1000 s2: 123.321 s3: true s4: A s5: 1000123.321

String static Method valueOf String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); Method valueOf converts the provided parameter and returns a string. Four overloaded valueOf methods are displayed. Note that the valueOf method is a static method (or class method) that is called with the String class identifier.

// Java1212.java // This program converts string values to integer and double values // using the <parseInt> and <parseDouble> methods of the <Integer> // and <Double> classes. public class Java1212 { public static void main (String args[]) System.out.println("\nJava1212.java\n"); String s1 = "123456"; String s2 = "123.456"; int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); double sum = n1 + n2; System.out.println(n1 + " + " + n2 + " = " + sum); System.out.println(); } Java1212.java 123456 + 123.456 = 123579.456

Integer static method parseInt and Double static method parseDouble int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); Method parseInt converts a String into an int. Method parseDouble converts a String into a double. Parameters that include non-numerical characters will compile, but will cause a run-time error.

Section 12.7 Comparing Strings

Enter a string ===>> Foxtrot Foxtrot does not equal Waltz // Java1213.java // This program checks equality of strings using the == operator. // The == operator does not work correctly with string objects. public class Java1213 { public static void main (String args[]) System.out.println("\nJava1213.java\n"); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = Expo.enterString(); if (s1 == s2) System.out.println(s1 + " equals " + s2); else System.out.println(s1 + " does not equals " + s2); if (s1 == s3) System.out.println(s1 + " equals " + s3); System.out.println(s1 + " does not equals " + s3); System.out.println(); } Java1213.java Enter a string ===>> Foxtrot Foxtrot does not equal Waltz Foxtrot does not equal Foxtrot

Enter a string ===>> Foxtrot Foxtrot does not equal Waltz // Java1214.java // This program demonstrates the <equals> method, which is capable of // testing equality of string objects correctly.   public class Java1214 { public static void main (String args[]) System.out.println("\nJava1214.java\n"); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = Expo.enterString(); if (s1.equals(s2)) System.out.println(s1 + " equals " + s2); else System.out.println(s1 + " does not equals " + s2); if (s1.equals(s3)) System.out.println(s1 + " equals " + s3); System.out.println(s1 + " does not equals " + s3); System.out.println(); } Java1214.java Enter a string ===>> Foxtrot Foxtrot does not equal Waltz Foxtrot equals Foxtrot

What Is Going On? Part 1 The equality operator == works with int x = 10; int y = 10; int z = 20; x 10 y 10 z 20 The equality operator == works with primitive data types like int. x == y x != z

What Is Going On? Part 2 s1 @dff6ccd s2 @3b0eb0 s3 @18d107f dff6ccd Foxtrot 3b0eb0 Waltz 18d107f Foxtrot The equality operator == does not work with objects because it compares the Shallow Values which are memory addresses.

What Is Going On? Part 3 s1 @dff6ccd s2 @3b0eb0 s3 @18d107f dff6ccd Foxtrot 3b0eb0 Waltz 18d107f Foxtrot The equals method should be used with objects like Strings because it compares the Deep Values which is the actual information stored.

The Bottom Line If you are comparing simple data types like 2 ints, 2 doubles, 2 chars or 2 booleans, use the == operator. If you are comparing objects – and Strings are objects – you need to use the equals method.

Java1215.java value1: -25 value2: 0 value3: 25 value4: -1 // This program demonstrates the <compareTo> method, which returns an integer value. // The returned value indicates which string alphabetically goes before the other. // If the value is negative, the original string goes first. // If the value is positive, the parameter string goes first. // If the value is zero, both strings are equal. public class Java1215 { public static void main (String args[]) System.out.println("\nJava1215.java\n"); String s1 = "AARDVARK"; String s2 = "ZEBRA"; String s3 = "AARDVARK"; String s4 = "BART"; int value1 = s1.compareTo(s2); int value2 = s1.compareTo(s3); int value3 = s2.compareTo(s1); int value4 = s1.compareTo(s4); System.out.println("value1: " + value1); System.out.println("value2: " + value2); System.out.println("value3: " + value3); System.out.println("value4: " + value4); System.out.println(); } Java1215.java value1: -25 value2: 0 value3: 25 value4: -1

public static void main (String args[]) // Java1216.java // This program shows the <compareTo> method being used to alphabetize 2 strings. // Note: This will not work properly if the strings are not all in the same "case". public class Java1216 { public static void main (String args[]) System.out.println("\nJava1216.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); System.out.print("Enter second string ===>> "); String s2 = Expo.enterString(); System.out.println(); if (s1.compareTo(s2) < 0) System.out.println(s1 + " goes before " + s2); else if (s1.compareTo(s2) > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); } Java1216.java Enter first string ===>> apple Enter second string ===>> banana apple goes before banana Java1216.java Enter first string ===>> zebra Enter second string ===>> neon neon goes before zebra Java1216.java Enter first string ===>> qwerty Enter second string ===>> qwerty Both strings are equal Java1216.java Enter first string ===>> apple Enter second string ===>> ZEBRA ZEBRA goes before apple

Note that the value of s1 changed from “apple” to “APPLE”. // Java1217.java // This program cures the "case" issue of the previous program. // <toUpperCase> is used to convert all strings to CAPITAL letters so case-sensitivity is // no longer an issue. The <toLowerCase> methods could have been used just as well. public class Java1217 { public static void main (String args[]) System.out.println("\nJava1217.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); System.out.print("Enter second string ===>> "); String s2 = Expo.enterString(); System.out.println(); s1 = s1.toUpperCase(); s2 = s2.toUpperCase(); if (s1.compareTo(s2) < 0) System.out.println(s1 + " goes before " + s2); else if (s1.compareTo(s2) > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); } Java1217.java Enter first string ===>> apple Enter second string ===>> ZEBRA APPLE goes before ZEBRA Note that the value of s1 changed from “apple” to “APPLE”.

Note that the value of s1 did not change this time. // Java1218.java // This program shows an alternate cure for the "case" issue of program Java1216.java. // With this solution, the 2 strings do not get altered as with the previous program. public class Java1218 { public static void main (String args[]) System.out.println("\nJava1218.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); System.out.print("Enter second string ===>> "); String s2 = Expo.enterString(); System.out.println(); int difference = s1.toUpperCase().compareTo(s2.toUpperCase()); if (difference < 0) System.out.println(s1 + " goes before " + s2); else if (difference > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); } Java1218.java Enter first string ===>> apple Enter second string ===>> ZEBRA apple goes before ZEBRA Note that the value of s1 did not change this time.

String methods equals and compareTo if (s1.equals(s2)) int difference = s3.compareTo(s4); Method equals returns true if s1 is equal to s2, and false otherwise. Method compareTo returns an int value based on the difference between s3 and s4. If the int value is 0, s3 and s4 are equal. If the int value is negative, s3 goes before s4. If the int value is positive, s3 goes after s4.