CSC1110 - Week 14 Exercise
Overview
In this Exercise you will get practice reading and writing from text files.
Resources
- Accept the GitHub Classroom assignment invitation in Canvas and then, in IntelliJ, create a new project from Version Control using the repository URL.
Instructions
Implement the following methods based on their descriptions and the following.
- All text files will be written and read from the words folder in the repository. The words folder is in the root directory of the project folder.
- When asking the user for a filename, assume they will enter the full relative path. For example, "words/inputFile.txt".
- Any Exceptions thrown by readArrayList and writeArrayList should cause the program to terminate gracefully (i.e., not crash) and print a descriptive error message.
readArrayList(String filename, int num, ArrayList<String> words)
- This method reads the first num elements from the file specified by the filename and adds them to the ArrayList.
You can assume the elements are on the same line, separated by a space.
- If the file contains less that num elements, it reads and adds all of them to the ArrayList.
- This method throws an IllegalArgumentException if the passed-in num is non-positive.
- This method throws a FileNotFoundException if an error occurs while reading the file.
writeArrayList(String filename, ArrayList<String> words)
- Converts each String in the passed-in ArrayList to an int and writes its squared value to the file specified by
the filename. The numbers should be written to the same line with a space between each.
- If an error occurs converting a String to an int, the program should print a descriptive message and move onto the next String in the ArrayList.
- This method throws a FileNotFoundException if an error occurs while writing to the file.
main()
- Creates a Scanner and ArrayList of Strings.
- Ask the user for the input filename and number of elements to read.
- Call readArrayList and pass in the input filename, number of elements, and the ArrayList of Strings.
- Ask the user for the output filename.
- Call writeArrayList and pass in the output filename and the ArrayList of Strings.
- Add any try/catch statements to ensure the program terminates gracefully if any of the previously mentioned, program-ending Exceptions occur.
Submission Instructions
Commit and push your code to your repository.