1 / 18

Strings and Text I/O

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 (“[ ,;]");

lucia
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. 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!!

  3. 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.

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

  5. 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’;

  6. 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

  7. 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

  8. 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)

  9. 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.

  10. StringBuilder Constructors • String s = new String(“Welcome”); • StringBuilder s = new StringBuilder(); • StringBuilder s = new StringBuilder(“Welcome”);

  11. 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();

  12. Other methods • s.length() • s.charAt(int index) • s.toString()

  13. 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)

  14. 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

  15. 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).

  16. 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.

  17. 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

  18. 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.

More Related