1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.

Download 1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.

Post on 17-Jan-2016

215 views

Category:

Documents

0 download

TRANSCRIPT

<p>Chapter 3d Scanner</p> <p>Building Java ProgramsChapter 3The string class</p> <p>#The equals methodStrings and Objects are compared using a method named equals.</p> <p>Scanner console = new Scanner(System.in);System.out.print("What is your name? ");String name = console.next();if (name.equals("Barney")){ System.out.println("I love you, you love me,"); System.out.println("We're a happy family!");}#2IndexesCharacters of a string are numbered with 0-based indexes:</p> <p>String name = "R. Kelly";</p> <p>First character's index : 0Last character's index : 1 less than the string's length</p> <p>The individual characters are values of type char (seen later)index01234567characterR. Kelly#3String methodsThese methods are called using the dot notation:</p> <p>String gangsta = "Dr. Dre";System.out.println(gangsta.length()); // 7Method nameDescriptionstr1.indexOf(str2)index where the start of str2 appears in str1 (-1 if not found)str1.length()number of characters in this stringstr1.substring(index1, index2)orstr1.substring(index1)the characters in this string from index1 (inclusive) to index2 (exclusive);if index2 is omitted, grabs till end of stringstr1.toLowerCase()a new string with all lowercase lettersstr1.toUpperCase()a new string with all uppercase letters#4String method examples// index 012345678901String s1 = "Stuart Reges";String s2 = "Marty Stepp";</p> <p>System.out.println(s1.length()); // 12System.out.println(s1.indexOf("e")); // 8System.out.println(s1.substring(7, 10)); // "Reg"</p> <p>String s3 = s2.substring(1, 7);System.out.println(s3.toLowerCase()); // "arty s"</p> <p>Given the following string:</p> <p>// index 0123456789012345678901String book = "Building Java Programs";</p> <p>How would you extract the word "Java" ?#5Modifying stringsMethods like substring and toLowerCase build and return a new string, rather than modifying the current string.</p> <p>String s = "lil bow wow";s.toUpperCase();System.out.println(s); // lil bow wow</p> <p>To modify a variable's value, you must reassign it:</p> <p>String s = "lil bow wow";s = s.toUpperCase();System.out.println(s); // LIL BOW WOW</p> <p>An object that can never be modified after creation is called an immutable object. Strings are immutable.#String test methodsMethodDescriptionstr1.equals(str2)whether two strings contain the same charactersstr1.equalsIgnoreCase(str2)whether two strings contain the same characters, ignoring upper vs. lower casestr1.startsWith(str2)whether str1 contains str2's characters at startstr1.endsWith(str2)whether str1 contains str2's characters at endstr1.contains(str2)whether str1 contains str2s characters anywhere#7Try this</p> <p>Write a method that accepts a string parameter, and returns the string in reverse order.Example: reverse(Good day)should return yad dooG</p> <p>#ScannerFor most objects (including Scanner objects), we create a new instance with the new keyword:TypeName myInstance = new TypeName(any, parameters);For a Scanner, it looks like this:Scanner console = new Scanner(System.in);</p> <p>#9A bit more magic: importTheres one other thing we have to do before we can start using our Scanner. We have to tell Java where it can find it!We do this with one more magic Java keyword, import, at the top of the Java source code file:import java.util.*;#10ScannerWhat will this output?I dont know! It depends on what you type at runtime!import java.util.*;</p> <p>public class MyInteractiveProgram { public static void main(String[] args) { Scanner console = new Scanner(System.in);</p> <p> System.out.print("Type something: "); String word = console.next();</p> <p> System.out.print("The first word was: " + word); }}#11Lets try it!What is the radius? 3A circle with radius 3.0 has circumference 18.8495559215A circle with radius 3.0 has area 28.27433388230A sphere with radius 3.0 has volume 113.0973355292#Advanced work: ask for circle/square, ask for circumference/area/volume.12</p>