CSC 230: Elementary Data Structures and Algorithms
Assignment:
Create a package called homeWork2. This is a simple program that has only the one main class.
The program will have two parts to it (but it is all in one program). This program has you reading two files and writing to one file. You may choose whichever processes you wish to use to read the two files, but they must be different. You may choose how you wish to write the file. The files you will be reading will be text files. The file you will write to will be writing an array list.
The output shown on the screen and in the file must be readable.
The program must have error checking. In the main method, you will add a try-with-resources exception. Feel free to use the Java exception catch and/or to write your own messages.
Part 1: Reading the file called bakebread.txt
In the first part of the program, under the programmer and program information, you will ask a user to enter the name of the file to be opened. You will then test to see of the filename that was entered by the user is valid. If it is invalid, you will let the user reenter the filename. Once you have the correct name entered, you will be able to read the file.
The exists() method is most helpful.
The program will then print out the sentences that show the program information that will be the size of the file in bytes and the absolute path to the file.
You will then be able to ask the user which word he or she wishes to find in the file. Your program will then check to see if the word is in the file. If it is not, the program will say so. If the file does contain the word, the program will say how many times the word shows up in the file.
You will add a try to this part of the program before reading the contents of the file. Although you do not need to explicitly close the bakebread.txt file due to the try with resource. Under it, write the matching catch for the try you have.
Part 2 Reading the file called array.txt and writing to the file called arrayB.txt
In the second part of the program, you will create the ArrayList. You will then read the array into the list. After the array has been read, you will then add 7 more names to the list; 6 of these will be added to the end of the list (which is the default for the add() method and the 7th name will be added as the first name in the list. You will then, using the Collections class method, sort the array. Lastly, you will print the names to both the screen and the file.
There will be two try blocks in this part of the program. One will be before creation of the ArrayList and one will be before the reading the contents of the file. There will be two catches at the end of this code, one to handle the file and one to handle the array being out of bounds. Since you are using an ArrayList, this exception will not happen (ArrayLists can be resized).