Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in Java
Prev Contents / Index Next


Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from

  1. Open a text file so that you can read the file one line at a time. Read each line as a String and place that String object into a LinkedList. Print all of the lines in the LinkedList in reverse order.
  2. Modify Exercise 1 so that the name of the file you read is provided as a command-line argument.
  3. Modify Exercise 2 to also open a text file so you can write text into it. Write the lines in the ArrayList, along with line numbers (do not attempt to use the “LineNumber” classes), out to the file. Modify Exercise 2 to force all the lines in the ArrayList to uppercase and send the results to System.out.
  4. Modify Exercise 2 to take additional command-line arguments of words to find in the file. Print all lines in which any of the words match. Modify so that the FilenameFilter actually opens each file and accepts the file based on whether any of the trailing arguments on the command line exist in that file.
  5. Modify to produce all the file names in the current directory and subdirectories that satisfy the given regular expression. Hint: use recursion to traverse the subdirectories.
  6. Create a class called SortedDirList with a constructor that takes file path information and builds a sorted directory list from the files at that path. Create two overloaded list( ) methods that will either produce the whole list or a subset of the list based on an argument. Add a size( ) method that takes a file name and produces the size of that file.
  7. Modify so that it produces an alphabetic sort instead, using the tool from Chapter 11.
  8. Modify so that it uses a class containing a String and a count value to store each different word, and a Set of these objects to maintain the list of words.
  9. Modify so that it uses LineNumberReader to keep track of the line count. Note that it’s much easier to just keep track programmatically.
  10. Starting with section 4 of, write a program that compares the performance of writing to a file when using buffered and unbuffered I/O.
  11. Modify section 5 of to eliminate the spaces in the line produced by the first call to in5.readUTF( ). Repair the program as described in the text. In, copy the file and rename it to and rename the class Blip2 to BlipCheck (making it public and removing the public scope from the class Blips in the process). Remove the //! marks in the file and execute the program including the offending lines. Next, comment out the default constructor for BlipCheck. Run it and explain why it works. Note that after compiling, you must execute the program with “java Blips” because the main( ) method is still in class Blips.
  12. In, comment out the two lines after the phrases “You must do this:” and run the program. Explain the result and why it differs from when the two lines are in the program.
  13. (Intermediate) In Chapter 8, locate the example, which consists of four files. GreenhouseController contains a hard-coded set of events. Change the program so that it reads the events and their relative times from a text file. (Challenging: use a design patterns factory method to build the events—see Thinking in Patterns (with Java) at
  14. Create and test a utility method to print the contents of a CharBuffer up to the point where the characters are no longer printable.
  15. Experiment with changing the ByteBuffer.allocate( ) statements in the examples in this chapter to ByteBuffer.allocateDirect( ). Demonstrate performance differences, but also notice whether the startup time of the programs noticeably changes.
  16. For the phrase “Java now has regular expressions” evaluate whether the following expressions will find a match:

    1. Apply the regular expression


      "Arline ate eight apples and one orange while Anita hadn't any"

      1. Modify to accept flags as arguments (e.g., Pattern.CASE_INSENSITIVE, Pattern.MULTILINE).
      2. Modify to use Java nio memory-mapped files. Modify to accept a directory name or a file name as argument (if a directory is provided, search should include all files in the directory). Hint: you can generate a list of filenames with:
        String[] filenames = new File(".").list();

        [61] Design Patterns, Erich Gamma et al., Addison-Wesley 1995.

        [62] It’s not clear that this was a good design decision, especially compared to the simplicity of I/O libraries in other languages. But it’s the justification for the decision.

        [64] Chapter 13 shows an even more convenient solution for this: a GUI program with a scrolling text area.

        [65] Chintan Thakker contributed to this section.

        [66] A chapter dedicated to strings will have to wait until the 4th edition. Mike Shea contributed to this section.

        [67] I have no idea how they came up with this method name, or what it’s supposed to refer to. But it’s reassuring to know that whoever comes up with nonintuitive method names is still employed at Sun. And that their apparent policy of not reviewing code designs is still in place. Sorry for the sarcasm, but this kind of thing gets tiresome after a few years.

        Thinking in Java
        Prev Contents / Index Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire