Likes

Java Lab_14_Ans_1

1. Use the Files. readAllLines method to read the entire contents of two files: a form template, and a list of names to send form letters to. After creating a form letter with a name from the name list, you will use the Files, write method to create the custom letter. You will also use the Pattern and Matcher classes that you saw in the "String Processing" lesson.


SOLUTION


1. Open the file FornTerplate in the resource: director.-.
Note that this is a form letter with a string <NAHE> that will be replaced by a name from the name list file.
2. Open the file NamesList.txt in the resources directory.
a. This file contains the names to send the form letters to.
b. Add your name to the end of the list.
c. Save the file.
3. Open the project ForntetterM-iier in the practices directory.
4. Expand the FornietterKricer class. Notice that this class contains the mam method, and that the application requires two parameters: One is the path to the form letter template, and the second is the path to the file containing the list of names to substitute in the form letter.
a. After checking for a valid number of arguments, the mam method then checks to see whether the Path objects point to valid files.
b. The mam method creates an instance of the FilcMerge class with the form letter Path object and the list of names =ath object.
c. In a try block the mam method calls the write! lergedF©™ method of the FileMerge class. This is the method that you will write in this practice.
5. Expand the FileMerge class.
a. Note the write!tergedForns method is empty. This is the method that you will write m this practice.
b. The merge Name method uses the Pattern object defined in the field declarations to replace the string from the form template (first argument) with a name from the name list (second argument). It returns a Strmg. For example, it replaces "Dear <MAME>,” with "Dear Wilscn Ball,”.
c. The naiToker method returns a boolean to indicate whether the string pa»>ed m contains the token This is useful to identify which string has the token to replace with the name from the name list.
6. Code the writeMergedForms method. The overall plan for this method is to read in the entire form letter, line by line, and then read in the entire list of names and merge the names with the form letter, replacing the placeholder in the template with a name from the list and then writing that out as a file. The net result should be that if you have ten names m the name list, you should end up with ten custom letter fries addressed to the names from the name list. These ten files will be written to the resources director.-.
a. Read in all of the Imes of the form letter into the -ormLet-er field, and all of the Imes from the name list into the nans List field.
Note: Because i%rite!lergedForni throws IC€xcepticn. you do not need to put these statements into a try block. The caller of this method is responsible for handlmg air.- exceptions thrown.
b. Create a ■'or loop to iterate through the list of names (nareList) strings.
c. Inside this for loop, create a new List object to Isold the strings of the form letter. You need tins new Lis-: to hold the modified form template strings to write out.
d. Still inside the -or loop, you will need to create a name for the custom letter. One easy way to do this is to use the name from the name list. You should replace air.- spaces in the name with underscores for readability of the file name. Create a new Path object relative to the form template path.
e. Create another -or loop, nested in the first loop, to iterate through the lines of the form template and look for the token string
(" NAME ”) to replace with the String name from the naneList. Use the haiTo<en method to look for the String that contains the token string and replace that string with one containing the name from the naneList. Use the mergeNane method to create the new String. Add the modified String and all of the Other Strings from the -ormLerser to the new custonLetter List.
f. Still inside the first ■'or loop, write the modified List of Strings that represents the customized form letter to the file system by using the 'lies...rite method. Prmt a message that the file write was successful and close the outer for loop.
g. Save the File!-«rge class.
7. Modify the FomLetterUriter project to pass the form letter file and the name list file to the mam method.
a. Right-click the project and select Properties.
b. Select Run.
c. In the Arguments text field, enter:
D:\labs\resources\=or«n7emplane. tx« D:\labs\resources\NamesList.txt
d. Click OK.
S. Run the project. You should >ee new files created with the names from the name list. Each file should be customized with the name from the name list. For example, the Tcm_McGinn.txt file should contain:
Dear Ton McGinn,
It has ccme to our attention that you would like to prove your lava Skills. May we reconnend that you consider certification fron Oracle? Oracle has globally recognised Certification exans that will test your lava krsowlesge and skills.
Start with the Oracle Certified lava Associate exam, and then continue to the Oracle Certified lava Prograrner Professional for a ccrrplete certification profile.

No comments: