Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section 7.12 Chapter Summary

Subsection 7.12.1 Technical Terms

baseline concatenation copy cosntructor
data structure delimited string delimiter
empty string garbage collection glyph
lexicographic order logical font off-by-one error
orphan object physical font read only
string string index string literal
token unit indexed zero indexed

Subsection 7.12.2 Important Points}

  • A String literal is a sequence of 0 or more characters enclosed within double quotation marks. A String object is a sequence of 0 or more characters, plus a variety of class and instance methods and variables.
  • A String object is created automatically by Java the first time it encounters a literal string, such as “Socrates,” in a program. Subsequent occurrences of the literal do not cause additional objects to be instantiated. Instead, every occurrence of the literal “Socrates” refers to the initial object.
  • A String object is created whenever the new operator is used in conjunction with a String() constructor—for example, new String("hello").
  • The String concatenation operator is the overloaded \(+\) symbol; it is used to combine two String s into a single String: “hello” + “world” ==> “helloworld”. Strings are indexed starting at 0. The indexOf() and lastIndexOf() methods are used for finding the first or last occurrence of a character or substring within a String. The valueOf() methods convert a nonstring into a String. The length() method determines the number of characters in a String. The charAt() method returns the single character at a particular index position. The various substring() methods return the substring at particular index positions in a String.
  • The overloaded equals() method returns true if two String s contain the same exact sequence of characters. The == operator, when used on String s, returns true if two references designate the same String object. String objects are immutable. They cannot be modified.
  • A StringBuffer is a string object that can be modified using methods such as insert() and append().
  • A StringTokenizer is an object that can be used to break a String into a collection of tokens separated by delimiters. The whitespace characters—tabs, blanks, and newlines—are the default delimiters.
  • The FontMetrics class is used to obtain the specific dimensions of the the various Font s. It is useful when you wish to center text. Font s are inherently platform dependent. For maximum portability, it is best to use default fonts.

Solutions 7.12.3 Solutions to Self-Study Exercises

7.2 String Basics
7.2.1 Constructing Strings

Self-Study Exercises
7.2.1.1. What is printed?
Solution.
silly
7.2.1.2. What is printed?
Solution.
silly
7.2.1.3. Creating an empty string.
Solution.
A, B and D are correct. C is the blank, not the empty string.
7.2.1.4. String objects.
Solution.
(a) 1, (b) 1, (c) 3

7.2.2 Concatenating Strings

Self-Study Exercises
7.2.2.1. What is printed?
Solution.
silly stuff
7.2.2.2. Evaluate expressions.
Solution.
(a) 15, (b) “551”, “5175”

7.2.4 Converting Data to Strings

Self-Study Exercises
7.2.4.1. String.valueOf().
Solution.
(a) “45”, (b) “121”, (c) “X”

7.3 Finding Things Within a String
7.3.1 indexOf()

Self-Study Exercises
7.3.1.1. String indexOf() expressions.
Solution.
(a) 0, (b) 1, (c) -1
7.3.1.2. String indexOf() expressions.
Solution.
(a) 16, (b) “16”, (c) 1, (d) 15
7.3.1.3. String indexOf() expressions.
Solution.
(a) 1, (b) 13, (c) 7, (d) 3
7.3.1.4. String indexOf() expressions.
Solution.
(a) 7, (b) 7, (c) 3
7.3.1.5. Tricky indexOf() expression.
Solution.
Answer: 9 Solution:
tricky.indexOf(String.valueOf( tricky.indexOf("c") ));
tricky.indexOf(String.valueOf( 2 )) 
tricky.indexOf("2") 
9

7.4 Example: Keyword Search
7.4.2 Testing and Debugging

Self-Study Exercise
7.4.2.1. Keyword search.
Solution.
public static void main(String args[]) {
  KeywordSearch searcher = new KeywordSearch();
  String result = searcher.keywordSearch("this is a test","is");
  System.out.println(result);
  result = searcher.keywordSearch("able was i ere i saw elba","a"); 
  System.out.println(result);
  result = searcher.keywordSearch("this is a test","taste"); 
  System.out.println(result);        
}

7.5 From the Java Library: java.lang.StringBuffer

Self-Study Exercise
7.5.1. Revised Keyword Search.
Solution.
public static void main(String args[]) {
  KeywordSearch searcher = new KeywordSearch();
  String result = searcher.keywordSearch("this is a test","is");
  System.out.println(result);
  result = searcher.keywordSearch("able was i ere i saw elba","a"); 
  System.out.println(result);
  result = searcher.keywordSearch("this is a test","taste"); 
  System.out.println(result);        
}

7.6 Retrieving Parts of Strings
7.6.1 charAt() and subString() methods

Self-Study Exercises
7.6.1.1. Substring expressions.
Solution.
(a) “uvwxyz”, (b) “bcde”, (c) “xyz”, (d) “xy”, (e) “xyz”
7.6.1.2. Substring expressions 2.
Solution.
(a) “uvwxyz”, (b) “bcde”, (c) “xyz”, (d) “xyz”, (e) “xyz”

7.8 Processing Each Character in a String
7.8.5 Example: Capitalizing the First Letter

Self-Study Exercises
7.8.5.1. String processor.
Solution.
public static String removeBlanks(String s) {
  StringBuffer sb = new StringBuffer();
  for (int k = 0; k < s.length(); k++) {
    if (s.charAt(k) != ' ') {
      sb.append(s.charAt(k));
    }
  }
  return sb.toString();
}

7.9 Comparing Strings
7.9.1 Lexicographic Order

Self-Study Exercises
7.9.1.1. Lexicographic Order.
Solution.
The correct order is: A Alpha Zero a alpha beta zero
7.9.1.2. StringFollows.
Solution.
public class StringFollows {

   public static boolean follows(String s1, String s2) {
        int minlen = Math.min(s1.length(), s2.length()); // Pick shorter length
                        
        for (int k=0; k < minlen; k++) {     // For each ch in shorter string
           if (s1.charAt(k) != s2.charAt(k))        // If chars unequal
               return s1.charAt(k) > s2.charAt(k); //  return true if s1's ch > s2's
        }
                                            // If all characters so far are equal
                                         //  then s1 > s2 if it is longer than s2
    return s1.length() > s2.length();  
   }


   public static void main(String args[]) {

     // Add code to test your methods here
         System.out.println (follows("a", "ab") );
         System.out.println (follows("ab", "a") );
         System.out.println (follows("ab", "ab") );
   } // main()
} // StringFollows

7.9.3 String Identity Versus String Equality

Self-Study Exercises
7.9.3.1. String equality vs identity.
Solution.
(a) true, (b) true, (c) false, (d) false, (e) false
7.9.3.2. String equality vs identity Part 2.
Solution.
(a) true, (b) false, (c) false, (d) false
7.9.3.3. String Play.
Solution.
public static void main(String args[]) {
  String s1 = "abcdefghijklmnopqrstuvwxyz";
  String s2 = "hello world";

  // Combine parts of s1 and s2 to create a new string "hello abc".
  System.out.println(s2.substring(0,6)+s1.substring(0,3));
  //  Swap "world" and "hello" in s2 to give "world hello".
  System.out.println(s2.substring(6) + " " + s2.substring(0,5));
  // Swap the front and back halves of s1 to give:
  // nopqrstuvwxyzabcdefghijklm
  System.out.println(s1.substring(s1.indexOf('n')) + 
                     s1.substring(0,s1.indexOf('n')));
} // main()
You have attempted of activities on this page.