Java Reference
In-Depth Information
Your program can be invoked from the command line with the Java source-
code file as the argument. It converts the Java source code to a new format. For
example, the following command converts the Java source-code file
Test.java
to the end-of-line brace style.
java Exercise14_12 Test.java
*14.13
(
Count characters, words, and lines in a file
) Write a program that will count
the number of characters, words, and lines in a file. Words are separated by
whitespace characters. The file name should be passed as a command-line
argument, as shown in Figure 14.13.
F
IGURE
14.13
The program displays the number of characters, words, and lines in the
given file.
*14.14
(
Process scores in a text file
) Suppose that a text file contains an unspecified
number of scores separated by blanks. Write a program that prompts the user to
enter the file, reads the scores from the file, and displays their total and average.
Scores are separated by blanks.
*14.15
(
Write/read data
) Write a program to create a file named
Exercise14_15.txt
if
it does not exist. Write
100
integers created randomly into the file using text
I/O. Integers are separated by spaces in the file. Read the data back from the
file and display the sorted data.
**14.16
(
Replace text
) Listing 14.15, ReplaceText.java, gives a program that replaces
text in a source file and saves the change into a new file. Revise the program
to save the change into the original file. For example, invoking
java Exercise14_16 file oldString newString
replaces
oldString
in the source file with
newString
.
***14.17
(
Game: hangman
) Rewrite Exercise 9.25. The program reads the words
stored in a text file named
hangman.txt
. Words are delimited by spaces.
**14.18
(
Add package statement
) Suppose you have Java source files under the direc-
tories
chapter1
,
chapter2
,...,
chapter34
. Write a program to insert the
statement
package chapteri;
as the first line for each Java source file
under the directory
chapteri
. Suppose
chapter1
,
chapter2
,...,
chapter34
are under the root directory
srcRootDirectory
. The root
directory and
chapteri
directory may contain other folders and files. Use
the following command to run the program:
java Exercise14_18 srcRootDirectory
*14.19
(
Count words
) Write a program that counts the number of words in President
Abraham Lincoln's Gettysburg address from
http://cs.armstrong.edu/liang/