1 / 22

Strings and Text I/O

Strings and Text I/O. About the Midterm Exam. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring break. The String class. In Java, a string is an object. The String class has more than 40 methods.

rowena
Download Presentation

Strings and Text I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings and Text I/O

  2. About the Midterm Exam.. • Exam on March 12 Monday (Tentatively) • Review on March 7 Wednesday • Cover from Chapter 6 • Grades will be out before spring break.

  3. The String class • In Java, a string is an object. • The String class has more than 40 methods. • How to construct a string? • String message = new String(“Welcome to Java”); • String message = “Welcome to Java”;

  4. Strings are immutable. • A string object is immutable; its contents cannot be changed. • Does the code below change the contents of the string? • String s = “Java”; • s = “C++”;

  5. How to compare two strings? • String s1 = "hello"; • String s2 = new String("hello"); • if (s1 == s2) • //checks only if they refer to the same object • if (s1.equals(s2)) • if (s2.equals(s1)) • //checks if they have the same contents

  6. String comparisons • method startWith(prefix) returnsboolean • String s = “hello class”; • s.startsWith(“hello”); • s.startsWith(“class”); • method endsWith(suffix) returns boolean

  7. String length • You can get the length of a string by invoking its length() method. • String s = “hello class”; • s.length() • int[] array = new int[10]; • array.length

  8. Access individual characters in a string • method charAt(index) • String s = “hello”; • s.charAt(0) • s.charAt(s.length()-1) • Do not use s[0]!

  9. Access individual characters in a string

  10. Exercise 1 • Write a method that finds the number of occurrences of a specified character in the string: • public static intcountCharacter(String s, char a) • countCharacter(“Welcome”, ‘e’) should return 2.

  11. Exercise 2: Checking Palindromes • A string is a palindrome if it reads the same forward and backward. • mom, dad, noon, 12321 • Write a program that prompts the user to enter a string and reports whether it is a palindrome. • public static booleanisPalindrome(String s)

  12. String concatenation • String s1 = “hello”; • String s2 = “world”; • String s3 = s1.concat(s2); • String s3 = s1 + s2;

  13. Converting • String s = “Welcome”; • s.toLowerCase() returns “welcome” • s.toUpperCase() returns “WELCOME” • String s = “ Welcome “; • s.trim() returns “Welcome”

  14. Replacing • replaceAll(oldString, newString) • String s = “Welcome”; • s.replaceAll(“e”, “*”) returns “W*lcom*” • s.replaceAll(“[eo]”, “*”) returns “W*lc*m*” • Note it is different from s.replaceAll(“eo”, “*”);

  15. Exercise 3: Phone keypads • 1 2 3 • ABC DEF • 4 5 6 • GHI JKL MNO • 7 8 9 • PQRS TUV WXYZ • Enter a string: 1-800-FLOWERS • 1-800-3569377

  16. Splitting a string • The split method can be used to extract tokens from a string with the specified delimiters. • String s = "Michael David Harry Mark"; • String[] names = s.split(" "); • String s = "Michael David;Harry,Mark"; • String[] names = s.split(“[ ,;]"); • String[] names = s.split(“ ,;"); //totally different!!

  17. Finding a character or a substring in a string • String s = "Welcome to Java“; • s.indexOf('W') returns 0. • s.indexOf('x') returns -1. • s.indexOf("come") returns 3. • s.indexOf("Java") returns 11. • s.indexOf("java") returns -1. • s.indexOf(‘o’) returns 4. • s.indexOf(‘o’, 5) returns 9.

  18. Converting strings to numeric values • String s = "5"; • double a = Integer.parseInt(s); • String s = "5"; • double a = Double.parseDouble(s);

  19. The Character class • Java provides a wrapper class for every primitive data type. • Character, Boolean, Byte, Short, Integer, Long, Float, Double • They enable the primitive data values to be treated as objects. • Character c = new Character(‘a’); • char c = ‘a’;

  20. Comparing two characters • char c1 = ‘a’; • char c2 = ‘a’; • if (c1 == c2) // to compare primitive data • Character c1 = new Character('a'); • Character c2 = new Character('a'); • if (c1.equals(c2)) // to compare Character objects

  21. Methods in the Character class • Most of the methods in the Character class are static methods. • Character.isDigit(char c): boolean • Character.isLetter(char c): boolean • Character.isLetterOrDigit(char c): boolean • Character.toLowerCase(char c): char • Character.toUpperCase(char c): char

  22. Exercise 4: Checking password • Write a method that checks whether a string is a valid password. • A password mush have at least eight characters. • A password consists of only letters and digits. • A password must contain at least two digits.

More Related