refactor: remove the throw argument for open file, and add try catch

This commit is contained in:
Anthony Berg 2024-10-15 15:48:09 +02:00
parent 9b031e3da6
commit 0dbd6eb471

View File

@ -10,25 +10,30 @@ public class ReadFile {
* @param filename the name of the file with, only allowing a .txt extension
* @return The contents of the file
*/
public List<String> open(String filename) throws IllegalArgumentException, FileNotFoundException {
public List<String> open(String filename) {
// Check that the filetype is correct, otherwise, throw IllegalArgumentException
if (!filename.endsWith(".txt")) {
throw new IllegalArgumentException("The filename is not a .txt format");
}
// Open the file that was defined
File file = new File(filename);
Scanner reader = new Scanner(file);
Scanner reader;
try {
// Open the file that was defined
reader = new Scanner(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
// List for all the separate lines on the database
List<String> data = new ArrayList<>();
// Go through all the lines in the file and append them to the list `data`
while (reader.hasNextLine()) {
String currentData = reader.nextLine();
data.add(currentData);
}
return data;
}
}