220 likes | 303 Views
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.
E N D
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. • How to construct a string? • String message = new String(“Welcome to Java”); • String message = “Welcome to Java”;
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++”;
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
String comparisons • method startWith(prefix) returnsboolean • String s = “hello class”; • s.startsWith(“hello”); • s.startsWith(“class”); • method endsWith(suffix) returns boolean
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
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]!
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.
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)
String concatenation • String s1 = “hello”; • String s2 = “world”; • String s3 = s1.concat(s2); • String s3 = s1 + s2;
Converting • String s = “Welcome”; • s.toLowerCase() returns “welcome” • s.toUpperCase() returns “WELCOME” • String s = “ Welcome “; • s.trim() returns “Welcome”
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”, “*”);
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
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!!
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.
Converting strings to numeric values • String s = "5"; • double a = Integer.parseInt(s); • String s = "5"; • double a = Double.parseDouble(s);
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’;
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
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
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.