This exercise will help you practice file and screen I/O, and some
string manipulation in Java. Your program will start by reading in
two file names from the screen. To do this, put out a prompt for
each file name, as in:
Enter first file name: testfile1.dat
Enter second file name: testfile2.dat
The actual file names entered in the example above are
testfile1.dat and testfile2.dat. Each of these files is
a text file. Each line in such a file will contain
a single word, with perhaps some whitespace
before or after each word. (No line will be completely blank).
For example, the contents of
testfile1.dat might be:
blah
conservation
madam
and the contents of testfile2.dat might be:
blah
conversation
blah
radar
What you need to do is identify the following cases, whenever it
occurs:
- CASE 1: Whenever a word in either file is a palindrome,
identify the word, the file, and line number it came from.
- CASE 2: Whenever a word in one file is identical to a word
in the second file, identify both words, the
files, and line numbers they came from.
Your program will write output to a text file called results.txt. Also,
put all the CASE 1 examples together, then all the CASE 2 examples,
then all the CASE 3 examples. So, your results.txt file
for the above example files should be:
CASE 1:
"madam" on line 3 in testfile 1 is a palindrome.
"radar" on line 4 in testfile 2 is a palindrome.
CASE 2:
"blah" on line 1 in testfile1 is identical to "blah" on line 1 in testfile2
"blah" on line 1 in testfile1 is identical to "blah" on line 3 in testfile2
There is no code to download for this exercise. You are free to
use code in the course notes in any which way you please. Note that
you will need to remove whitespace in front of and after words, if present.
Submission:
- As usual,
you will need to follow
submission instructions carefully.
This time,
the name of subdirectory for this exercise should be: your username
followed by 2.
- You may use additional files, but make sure the source
is included in your jar file.
- Submit a printout (hardcopy) of your code before
the due date in my mailbox.
- What did you do to ensure your program is working correctly?
Include test cases and corresponding output in your hardcopy
submission.
- Your program should have a debug variable which can be
set to true or false. If set to true it
should print out (to the screen) debugging details (such as what line of what file
it's currently reading, the details of a comparison etc).
If set to false nothing should be printed to screen.
- Hint: you will find several methods in the
String
class useful.
For example, take a look at the description of the methods
trim()
and
toCharArray().
Remember, to get a description of a particular library method/class,
use the local documentation described on the homepage.
Note that
String
is in the package
java.lang.
The
String
class is also described in the Nutshell book.