180 likes | 275 Views
Strings and Text I/O. 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 (“[ ,;]");
E N D
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"; • inta = Integer.parseInt(s); • String s = "5"; • double b = 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. • public static booleancheckPassword(String s)
The StringBuilder/StringBuffer Class • an alternative to the String class • more flexible than String • You can add, insert, or append new contents into a string builder. • A String object is fixed.
StringBuilder Constructors • String s = new String(“Welcome”); • StringBuilder s = new StringBuilder(); • StringBuilder s = new StringBuilder(“Welcome”);
Modifying StringBuilder • StringBuilders = new StringBuilder(“Welcome”); • s.append(" "); • s.append("to"); • s.append(" "); • s.append("Java!"); • s.insert(11, "HTML and "); • s.delete(11,20); //delete from index 11 to index 20-1 • s.deleteCharAt(11); • s.reverse();
Other methods • s.length() • s.charAt(int index) • s.toString()
Exercise 5: Checking Palindrome • 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)
Command-Line Arguments • public static void main (String[] args) • What is that args?? • Example: passing two strings to the main method • java TestString hello class • java TestString 50 60
The File class • File name: test.txt • Absolute file name: C:\temp\test.txt • import java.io.File; • File f = new File(“test.txt”); • File f = new File(“C:\\temp\\test.txt”); • Backslash in Java is \\ (escape sequence).
The File class • The File class contains the methods for obtaining file properties and for renaming and deleting files. • f.exists(): boolean • f.getName(): String • f.getAbsolutePath (): String • f.delete(): boolean • The File class does NOT contain the methods for reading and writing file contents.
File Input and Output • Java I/O classes: Scanner and PrintWriter • import java.io.PrintWriter; (or java.io.*) • File f = new File("scores.txt"); • PrintWriter pw = new PrintWriter(f); • creates a new file if the file does not exist • discards the current content in the file if the file already exists
The PrintWriter class • pw.print(s: String): void • pw.println(s: String): void • //System.outis a standard Java object for the console. • pw.close(); //must be used to close the file. Otherwise the data may not be saved properly.