Unit 10 The String class 1 AP Computer











- Slides: 11

Unit 10 - The String class 1 AP Computer Science A – Healdsburg High School

The String Class Def. String – a collection of characters stored as one object. Ex. A P 0 1 2 C o m p u t e r 3 4 5 6 7 8 9 10 Strings are immutable. That is once a String object is created, none of its methods can change it. AP Computer Science A – Healdsburg High School

The String Class (continued) java. lang. String implements java. lang. Comparable What does this mean? ? Comparable is an interface, meaning that there are methods defined, but not written for the class. interface java. lang. Comparable int compare. To(Object other) // return value < 0 if this is less than other // return value = 0 if this is equal to other // return value > 0 if this is greater than other AP Computer Science A – Healdsburg High School

The String Class (continued) How are Comparable objects used? ? Ex. Write a method that returns the greatest of two String objects (in lexicographical order) public String greatest (String s 1, String s 2) { if(s 1. compare. To(s 2) > 0) return s 1; else return s 2; } AP Computer Science A – Healdsburg High School

The String Class (continued) Other String methods. int length() // returns the # of characters in String substring(int from, int to) // returns the substring beginning at from // and ending at to-1 String substring(int from) // returns substring(from, length()) int index. Of(String s) // returns the index of the first occurrence of s; // returns -1 if not found See page 265 in your book for a list of other methods AP Computer Science A – Healdsburg High School

String Constructors • String’s no-args and copy constructors are not used much. String s 1 = new String (); String s 1 = ""; String s 2 = new String (s 1); String s 2 = s 1; • Not to be confused with an uninitialized string: private String error. Msg; 6 error. Msg is null AP Computer Science A – Healdsburg High School

The String Class (continued) Ex. Analyze the following code segment: String s = "Help me, I'm lost!!"; int a = s. length(); String b = s. substring(5, 14); String c = s. substring(13); int d = s. index. Of("me"); int e = s. index. Of("z"); AP Computer Science A – Healdsburg High School

Escape Sequences Sequence n t ’ ” \ Meaning newline tab single quote double quote backslash System. out. print(“n. Don’t let me down”); AP Computer Science A – Healdsburg High School

Input/Output (I/O) from the console screen in Java (the little black screen) One input class: Easy. Reader. java Examples Easy. Reader console = new Easy. Reader(); String last. Name = console. read. Word(); String full. Name = console. read. Line(); int age = console. read. Int(); double GPA = console. read. Double(); AP Computer Science A – Healdsburg High School

File Input The last slide used Easy. Reader to read from the keyboard. Now we will use it to read from a file. How? ? Easy. Reader in. File = new Easy. Reader(filename); while(!in. File. eof()) // for char, int, double { char c = in. File. read. Char(); … do stuff } Easy. Reader in. File = new Easy. Reader(filename); String s; while((s = in. File. read. Word()) != null) // for Strings { … do stuff } AP Computer Science A – Healdsburg High School

File Input (continued) Ex. Open a file that contains text and count how many words contain the character ‘e’. Easy. Reader in. File = new Easy. Reader(filename); String s; int count = 0; while((s = in. File. read. Word()) != null) { if(s. index. Of(“e”) != -1) count++; } System. out. println(“There are “ + count + “ words with ’e’ in the file”); AP Computer Science A – Healdsburg High School