From 0dbd6eb471f5e43823123861ca7c30c071eb1bc0 Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Tue, 15 Oct 2024 15:48:09 +0200 Subject: [PATCH] refactor: remove the throw argument for open file, and add try catch --- src/ReadFile.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ReadFile.java b/src/ReadFile.java index d947330..eb86761 100644 --- a/src/ReadFile.java +++ b/src/ReadFile.java @@ -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 open(String filename) throws IllegalArgumentException, FileNotFoundException { + public List 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 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; } }