LastWord Java Program
You will write a Java program with the file name LastWord.java. The purpose of this program is to scan through a file and find in the file:
1. The word that is closest to the beginning of the dictionary
2. The word that is closest to the end of the dictionary
3. The longest word
For the purpose of this program, a word is defined as a string of characters delimited by whitespace that starts with a letter and ends with a letter. If a string of character delimited by whitespace that starts with one or more non-letters, they should be trimmed until the first character is a letter. Same should be done to the trailing non-letters. For example, the string of character “quote” should be trimmed to quote, –dash– should be trimmed to dash. The non-letter enclosed by letters should be left alone. This definition of a word may make your English teachers cringe, but will be much easier to implement.
The lexicographical order of the words can be determined simply by using the compareToIgnoreCase() method of String class.
The input file name should be entered by a JOptionPane input dialog box.
2 Suggested Procedure
1. Start the program by popping up an input dialog box to ask user for a file name.
2. Instantiate a FileReader using the file name wrapped in a BufferedReader.
3. nstantiate a FileWriter wrapped in a BufferedWriter. The output file name may be hardcoded.
4. Test the program by reading the input file and write the input to the output file. Compare the input file and output file.
5. Instantiate a Scanner that scans the BufferedReader. By default the Scanner delimits the tokens by whitespaces. Write the scanned tokens, one per line, to the output file. Exam the output file.
6. Write a new public class named WordSmith. Create a static method trim() to remove the non-letters around the words. Test WordSmith.trim() until you are satisfied.
7. Back to LastWord, between Scanner output and write to output, insert a call to WordSmith.trim(). Create a test file with all the possible scenarios you can think of. It will be more effective if you write out the word before and after trim() is called side-by-side on the same line.
8. Consider sharing your test file at DocSharing.
9. Add the code to LastWord to find the first word, last word, and the longest word.
10. Use JOptionPane.showMessageDialog() to display the results. Show the file name used in the title of the dialog box.
Source file(s): LastWord.java and WordSmith.java